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!
Asked
Active
Viewed 1,313 times
3 Answers
1
Try creating the table. You should get an error: 1050 "table already exists"
.
Or you can try this: https://stackoverflow.com/a/1525801/2427840
1
mysql_
- functions are deprecated as of PHP 5.5.0. You should use MySQLi or 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]';
-
+1 for using the `information_schema` and specifying `mysql_` functions are deprecated. – PLPeeters Jun 02 '13 at 09:10
0
Simple use:-
$result = mysql_query("SHOW TABLES LIKE 'table_name'");
$tableExist = mysql_num_rows($result) > 0;
if($tableExist)
echo "Table Exist";

Vivek Sadh
- 4,230
- 3
- 32
- 49