PHP:警告:sort() 期望参数 1 是数组,给定资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6169146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Warning: sort() expects parameter 1 to be array, resource given
提问by ravi
I wanted to arrange the array of table list with sort() function but i am getting same kind of warning my code as follows :
我想用 sort() 函数排列表列表的数组,但我的代码收到相同类型的警告,如下所示:
<?PHP
require_once("lib/connection.php");
$result = mysql_query("SHOW TABLES FROM `st_db_1`");
sort($result);
foreach ($result as $result){
echo $result ;
}
?>
and the warning i am getting are :
我收到的警告是:
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
回答by vindia
The warning is pretty clear: mysql_query
does not return an array with results from the query, but a resource. You need a function like mysql_fetch_array()
to return the data you need (and on which you can perform a sort operation).
警告非常清楚:mysql_query
不返回带有查询结果的数组,而是返回一个资源。您需要一个函数mysql_fetch_array()
来返回您需要的数据(并且您可以在其上执行排序操作)。
See the manual for the use of mysql_query()
http://nl3.php.net/mysql_query
使用手册见mysql_query()
http://nl3.php.net/mysql_query
And maybe unrelated, but you can sort your results in MySQL right away by adding ORDER BY <fieldname>
to your query.
也许不相关,但您可以通过添加ORDER BY <fieldname>
到您的查询立即在 MySQL 中对您的结果进行排序。
回答by Trott
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";
}
回答by Raffael Luthiger
The variable $result is only a resource of the type result. You need to fetch then the data from the result set with e.g. mysql_fetch_assoc().
变量 $result 只是 result 类型的资源。您需要使用例如mysql_fetch_assoc()从结果集中获取数据。
$result = mysql_query("SHOW TABLES FROM `st_db_1`");
$array = array();
while ($row = mysql_fetch_assoc($result)) {
$array[] = $row["Tables_in_st_db_1"];
}
sort($array);
foreach ($array as $item) {
echo $item;
}
回答by Spudley
Your problem is that you aren't actually getting the data from the query.
您的问题是您实际上并未从查询中获取数据。
mysql_query()
doesn't give you a recordset.
mysql_query()
没有给你一个记录集。
What it does is query the database and returns a database resource which you can then use to get the data.
它所做的是查询数据库并返回一个数据库资源,然后您可以使用它来获取数据。
What you need is after calling mysql_query()
, you then need to also call mysql_fetch_array()
or similar. (there are a range of functions available, but that's probably the best one to use in this case). Then sort()
the data from that, not $result
.
您需要的是在调用之后mysql_query()
,您还需要调用mysql_fetch_array()
或类似的方法。(有一系列可用的功能,但在这种情况下这可能是最好的功能)。然后sort()
来自那个的数据,而不是$result
.
回答by vbence
It clearly says: it expects an array and you pass something else.
它清楚地表明:它需要一个数组,而您传递的是其他东西。
If you had checked the type of $result
you would have seen that it is not an array, intead a resource.
如果您检查了类型,$result
您会发现它不是数组,而是资源。