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 - Manual: mysql_tablename
2024-12-21
(PHP 4, PHP 5)
mysql_tablename — 取得表名
本扩展自 PHP 5.5.0 起已废弃,并在自 PHP 7.0.0 开始被移除。应使用 MySQLi 或 PDO_MySQL 扩展来替换之。参见 MySQL:选择 API 指南来获取更多信息。用以替代本函数的有:
SHOW TABLES
$result
, int $i
): string|false
从 result
中取得表名。
此函数已废弃,推荐使用
mysql_query() 函数来执行 SQL SHOW TABLES
[FROM db_name] [LIKE 'pattern']
替代。
成功时返回表名 或者在失败时返回 false
。
Use the mysql_tablename() function to traverse this result pointer, or any function for result tables, such as mysql_fetch_array().
版本 | 说明 |
---|---|
5.5.0 |
mysql_tablename() 函数已废弃,调用时会抛出
E_DEPRECATED 级别的错误。
|
示例 #1 mysql_tablename() example
<?php
mysql_connect("localhost", "mysql_user", "mysql_password");
$result = mysql_list_tables("mydb");
$num_rows = mysql_num_rows($result);
for ($i = 0; $i < $num_rows; $i++) {
echo "Table: ", mysql_tablename($result, $i), "\n";
}
mysql_free_result($result);
?>
注意:
The mysql_num_rows() function may be used to determine the number of tables in the result pointer.
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;
}
官方地址:https://www.php.net/manual/en/function.mysql-tablename.php