0

I wanted to arrange the array of table list with sort() function but i am getting same kind of warning.

<?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

Dharman
  • 30,962
  • 25
  • 85
  • 135
ravi
  • 109
  • 2
  • 4
  • 13
  • 4
    You need to [fetch](http://php.net/manual/en/function.msql-fetch-array.php) the result first. E.g. using `$row = mysql_fetch_array($result):` – Jürgen Thelen May 29 '11 at 17:16
  • 2
    you need to lookup on what `mysql_query` returns, as well as `mysql_fetch_assoc`. By the way you'll need to use `print_r` instead of `echo` to see the structure of. – onteria_ May 29 '11 at 17:17
  • 1
    As well in your foreach loop, you are overwriting the `$result` variable which is probably not what you want at all. – judda May 29 '11 at 17:26

5 Answers5

2

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).

See the manual for the use of 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.

vindia
  • 1,678
  • 10
  • 14
2

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";
 }
Trott
  • 66,479
  • 23
  • 173
  • 212
  • Yup, moved the sort to the right place. That's what I get for not testing before posting. Uh, will go test now to make sure I didn't just make it worse. – Trott May 29 '11 at 17:32
  • No problem, the main problem is that the person asking this question was not fetching the rows. And you showed this. Now we have almost the same answer. I gave you a point and might delete my answer later because they are too similar. – Raffael Luthiger May 29 '11 at 18:02
2

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 = 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;
}
Raffael Luthiger
  • 2,191
  • 3
  • 19
  • 31
1

Your problem is that you aren't actually getting the data from the query.

mysql_query() doesn't give you a recordset.

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.

Spudley
  • 166,037
  • 39
  • 233
  • 307
0

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.

kapa
  • 77,694
  • 21
  • 158
  • 175
vbence
  • 20,084
  • 9
  • 69
  • 118
  • i checked ,,its a array if table names but i am successfully able to fetch array of table using while loop – ravi May 29 '11 at 17:21