[thelist] Remedial PHP - MySQL

Burhan Khalid thelist at meidomus.com
Thu Feb 17 00:07:16 CST 2005

Jay Blanchard wrote:
> [snip]
> I have several mysql databases. I'm trying, without a lot of success 
> this morning (must be tired), to use PHP to get a list of the tables 
> in one of those databases:
>      if (!($listOfTables = mysql_list_tables($link, 1))) {
>      	echo("Dang! No Tables Found!");
>      	}

Right here is your problem. First, you need to select a database to use 
with mysql_select_db(), then use this instead of mysql_list_tables():

$query  = "SHOW TABLES";
$result = mysql_query($query);
if (!$result) { die($query."<br />".mysql_error()); }
while($row = mysql_fetch_assoc($result))
    echo "<pre>"; print_r($row); echo "</pre>";

 From http://www.php.net/mysql-list-tables

  /The function mysql_list_tables() is deprecated. It is preferable to 
use mysql_query() to issue a SQL SHOW TABLES [FROM db_name] [LIKE 
'pattern'] statement instead./

If you want to show tables from a particular database instead of the one 
currently selected by mysql_select_db() (assuming the user has 
appropriate rights), you can issue :

$query = "SHOW TABLES FROM `db`";


> I think my syntax is off. I'm not sure how to point this command at 
> one database ... or does it return all of the tables from all of the 
> databases and I have to segregate them out of the results set?

This command returns all tables in the selected database, which you need 
to select by mysql_select_db() -- or, alternately modify your program as 
listed above.

More information about the thelist mailing list