PHP:检查MySQL TABLE是否已作为错误条件存在? [重复]

时间:2022-04-03 12:58:10

This question already has an answer here:

这个问题在这里已有答案:

How in PHP would I check for a condition if a MySQL TABLE already exists and it shouldn't and need to abort but with a message that's meaningful? Thanks!

如果MySQL TABLE已经存在并且它不应该并且需要中止但是有一个有意义的消息,我将如何在PHP中检查条件?谢谢!

3 个解决方案

#1


1  

Try creating the table. You should get an error: 1050 "table already exists".

尝试创建表格。你应该得到一个错误:1050“表已经存在”。

Or you can try this: https://*.com/a/1525801/2427840

或者你可以试试这个:https://*.com/a/1525801/2427840

#2


1  

mysql_ - functions are deprecated as of PHP 5.5.0. You should use MySQLi or PDO.

mysql_ - 自PHP 5.5.0起不再使用函数。您应该使用MySQLi或PDO。

To check if a table exists you can use this query:

要检查表是否存在,您可以使用此查询:

SELECT COUNT(*)
FROM information_schema.tables 
WHERE table_schema = '[database-name]' 
AND table_name = '[table-name]';

#3


0  

Simple use:-

$result = mysql_query("SHOW TABLES LIKE 'table_name'");
$tableExist = mysql_num_rows($result) > 0;
if($tableExist)
echo "Table Exist";

#1


1  

Try creating the table. You should get an error: 1050 "table already exists".

尝试创建表格。你应该得到一个错误:1050“表已经存在”。

Or you can try this: https://*.com/a/1525801/2427840

或者你可以试试这个:https://*.com/a/1525801/2427840

#2


1  

mysql_ - functions are deprecated as of PHP 5.5.0. You should use MySQLi or PDO.

mysql_ - 自PHP 5.5.0起不再使用函数。您应该使用MySQLi或PDO。

To check if a table exists you can use this query:

要检查表是否存在,您可以使用此查询:

SELECT COUNT(*)
FROM information_schema.tables 
WHERE table_schema = '[database-name]' 
AND table_name = '[table-name]';

#3


0  

Simple use:-

$result = mysql_query("SHOW TABLES LIKE 'table_name'");
$tableExist = mysql_num_rows($result) > 0;
if($tableExist)
echo "Table Exist";