我正在尝试检查表是否存在,如果存在,则执行一些操作。我不断地收到一个错误,告诉我这个表不存在,而不是完成我的检查。以下是代码:
$tableExists = $db->prepare("SHOW TABLES LIKE $table_array");
$tableExists->execute();
if($tableExists->rowCount() > 0) {
// do some code
} else {
echo "Unable to add because table does not exists";
}更新:根据下面的建议,我现在做以下工作:
$tableExists = $db->prepare("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?");
$tableExists->execute(array($table_array));
if(!is_null($tableExist)) {
//do something
} else {
echo "table does not exist;
}但是,if语句似乎无法确定表是否存在。我还能做什么?
发布于 2013-08-05 22:17:33
尝试使用information_schema来询问表是否存在。有点像
SELECT
*
FROM
information_schema
WHERE TABLE_NAME = "$table_array" 查看一下information_schema保存的所有内容,您会对它存储的数据库信息感到惊喜:)
发布于 2013-08-05 22:15:41
if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))==1)
echo "Table exists";
else echo "Table does not exist";发布于 2013-08-05 22:24:16
试试这个:
select case when (select count(*) from INFORMATION_SCHEMA.TABLES where TABLE_NAME='offices') = 1 then 'exists' else 'does not exist' endhttps://stackoverflow.com/questions/18068671
复制相似问题