在PHP中,查看表是否存在的最佳方法是什么?
这就是我到目前为止所使用的
public function TableExists($table) {
$res = $this->Query("SELECT 1 FROM $table");
if(isset($res->num_rows)) {
return $res->num_rows > 0 ? true : false;
} else return false;
}
Run Code Online (Sandbox Code Playgroud)
Col*_*n M 14
如果表不存在,您发布的内容将抛出错误.试试这个:
SHOW TABLES LIKE 'tablename';
Run Code Online (Sandbox Code Playgroud)
并确保您完全退回一行.
小智 10
科林有正确的解决方案 - 使用SHOW TABLES LIKE.以下是使用您的代码的样子:
public function TableExists($table) {
$res = $this->Query("SHOW TABLES LIKE $table");
return mysql_num_rows($res) > 0;
}
Run Code Online (Sandbox Code Playgroud)