tags:

views:

49

answers:

2

I am using SHOW TABLES to retrieve a list of tables in the DB. The DB has 19 tables

$db = mysql_connect($dbhost, $dbuser, $dbpassword);
if (!$db)
  {
  die('Could not connect: ' . mysql_error());
  }
$dbselect = mysql_select_db($dbase,$db);
if(!$dbselect) {
  die('Could not connect: ' . mysql_error());
}
$c_query=mysql_query("SHOW TABLES ",$db);
var_dump(mysql_fetch_array($c_query));

The OUTPUT only gives an array with the first table

array(2) { [0]=>  string(5) "tabl1" ["Tables_in_dbase"]=>  string(5) "tabl1" } 

Why? How do I retrieve a list of all tables in the db? Update: Looping seems to be the answer. There does not appear to be a query which returns all the entries in one query.

+4  A: 

The result contains multiple rows (each table per row), try something like:

while($row = mysql_fetch_array($c_query)) {
    var_dump($row);
}

See also the result of the query in phpMyAdmin, that also lists multiple rows.

bouke
Ow! Why cannot I get the result in one query, without resorting to a loop? Thanks a lot!
abel
The result is in one query, the query results in a resultset. The resultset contains multiple records. You could iterate over the resultset (as demonstrated) and add them to an array to do whatever you like with it.
bouke
+2  A: 

mysql_fetch_array returns the next row of the results as an array. It does not return the full result set as a 2D array.

You need to wrap that line in a loop:

while($row = mysql_fetch_array($c_query)) {
    var_dump($row);
}
Ty W
Thank you. Is there one which gives the entire result set? mysql_ fetch_ array mysql_ fetch_ assoc mysql_ fetch_ object mysql_ fetch_ row all seem to return a single row.
abel