MySQL check if a table exists without throwing an exception

clops picture clops · Oct 6, 2009 · Viewed 102.2k times · Source

What is the best way to check if a table exists in MySQL (preferably via PDO in PHP) without throwing an exception. I do not feel like parsing the results of "SHOW TABLES LIKE" et cetera. There must be some sort of boolean query?

Answer

nickf picture nickf · Oct 6, 2009

I don't know the PDO syntax for it, but this seems pretty straight-forward:

$result = mysql_query("SHOW TABLES LIKE 'myTable'");
$tableExists = mysql_num_rows($result) > 0;