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!

0

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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 Comment

+1 for using the information_schema and specifying mysql_ functions are deprecated.
0

Simple use:-

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.