abel
abel

Reputation: 2383

MySQL: SHOW TABLES only returns the first table

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.

Upvotes: 1

Views: 1572

Answers (2)

Ty W
Ty W

Reputation: 6814

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);
}

Upvotes: 2

Bouke
Bouke

Reputation: 12138

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.

Upvotes: 4

Related Questions