I'm trying to show all the tables in my database. I've tried this:
$sql = "SHOW TABLES";
$result = $conn->query($sql);
$tables = $result->fetch_assoc();
foreach($tables as $tmp)
{
echo "$tmp <br>";
}
but it only gives me one table name in a database I know has 2. What am I doing wrong?
<?php
$dbname = 'mysql_dbname';
if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
echo 'Could not connect to mysql';
exit;
}
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "Table: {$row[0]}\n";
}
mysql_free_result($result);
?>
//Try This code is running perfectly !!!!!!!!!!
Try this:
SHOW TABLES FROM nameOfDatabase;
SHOW TABLE_NAME is not valid. Try SHOW TABLES
TD
SHOW TABLES only lists the non-TEMPORARY tables in a given database.
Source: Stackoverflow.com