PHP:警告:sort() 期望参数 1 是数组,给定资源 [英] PHP: Warning: sort() expects parameter 1 to be array, resource given

查看:33
本文介绍了PHP:警告:sort() 期望参数 1 是数组,给定资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 sort() 函数排列表列表的数组,但我收到了同样的警告我的代码如下:

I wanted to arrange the array of table list with sort() function but i am getting same kind of warning my code as follows :

 <?PHP 
     require_once("lib/connection.php"); 

     $result = mysql_query("SHOW TABLES FROM `st_db_1`");

     sort($result);
     foreach ($result as $result){
     echo $result ;
     } 
 ?>

我收到的警告是:

Warning: sort() expects parameter 1 to be array, resource given in C:\wamp\www\Copy (4)       of st_db_1\test_2.php on line 9
Warning: Invalid argument supplied for foreach() in C:\wamp\www\Copy (4) of st_db_1\test_2.php on line 10

推荐答案

我没有提供可以想象到的最高效的代码,但这应该可以说明发生了什么并解决您的问题:

I'm not providing the most efficient code imaginable, but this should make it clear what's going on and solve your problem:

 $result = mysql_query("SHOW TABLES FROM `st_db_1`");

 $my_array_of_table_names = array();
 while ( $row = mysql_fetch_array($result, MYSQL_NUM)) {
     $my_array_of_table_names[] = $row[0];
 }
 sort($my_array_of_table_names);

 foreach ($my_array_of_table_names as $table_name){
     echo "$table_name\n";
 }

这篇关于PHP:警告:sort() 期望参数 1 是数组,给定资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆