2

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!

Edward
  • 9,430
  • 19
  • 48
  • 71

3 Answers3

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

Community
  • 1
  • 1
Gimmy
  • 3,781
  • 2
  • 18
  • 27
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]';
PLPeeters
  • 1,009
  • 12
  • 26
Zaziki
  • 418
  • 2
  • 12
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