Another way to check if a table exists:
if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))==1) echo "Table exists";
else echo "Table does not exist";
(PHP 4, PHP 5)
mysql_tablename — 取得表名
$result
, int $i
) : stringmysql_tablename() 接受 mysql_list_tables() 返回的结果指针以及一个整数索引作为参数并返回表名。可以用 mysql_num_rows() 函数来判断结果指针中的表的数目。用 mysql_tablename() 函数来遍历此结果指针,或者任何处理结果表的函数,例如 mysql_fetch_array()。
Example #1 mysql_tablename() 例子
<?php
mysql_connect("localhost", "mysql_user", "mysql_password");
$result = mysql_list_tables("mydb");
for ($i = 0; $i < mysql_num_rows($result); $i++)
printf ("Table: %s\n", mysql_tablename($result, $i));
mysql_free_result($result);
?>
Another way to check if a table exists:
if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))==1) echo "Table exists";
else echo "Table does not exist";
A simple function to check for the existance of a table:
function TableExists($tablename, $db) {
// Get a list of tables contained within the database.
$result = mysql_list_tables($db);
$rcount = mysql_num_rows($result);
// Check each in list for a match.
for ($i=0;$i<$rcount;$i++) {
if (mysql_tablename($result, $i)==$tablename) return true;
}
return false;
}