...

View Full Version : PHP global var and mysql_list_tables()



binaryWeapon
08-06-2008, 10:55 PM
I'm trying to make a function to check if a certain table exists in the database, and this line is giving me trouble:
$tables = mysql_list_tables($db);
$num_tables = mysql_numrows($tables); When I manually hard-code a database name instead of putting the global var $db, it works. What am I doing wrong? I know for a fact that $db is accessible by the function and that it is the correct database name. If it matters, $db was declared inside another function as global $db. The error is this:
Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/nathan/public_html/testzone/database/testnewphp.php on line 16
Table does not exist. Line 16 is the second line in the function,
$num_tables = mysql_numrows($tables);

If it matters, the complete code for the function is this:
function table($table)
{
$tables = mysql_list_tables($db);
$num_tables = mysql_numrows($tables);
$i = 0;
$exist = 0;
while($i < $num_tables) {
$tablename = mysql_tablename($tables, $i);
if ($tablename==$table) $exist=1;
$i++;
}
if ($exist==0){
die("Table does not exist.");
} else {
$from=$table;
}
}

Brandoe85
08-06-2008, 11:23 PM
That seems like a lot of work to check for a table - I would take a different approach and run a mysql query and then do a num_rows on that like:
SHOW TABLES LIKE tablename

Wrap that in a mysql_query + mysql_num_rows.

Good luck;

binaryWeapon
08-06-2008, 11:47 PM
Thanks, that's a good solution and works better. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum