Checking that a table exists on MySQL
mysql, php
Solution
Try using the `information_schema` to ask if the table exists. Something like
SELECT
*
FROM
information_schema
WHERE TABLE_NAME = "$table_array"
Take a look through everything the `information_schema` holds, you will be pleasantly surprised by the information it has stored about your databases :)
Problem
I am trying to check whether a table exists, and if so then do some actions. I keep on getting an error telling me that the table does not exist rather than completing my check. Here is the code: ``` $tableExists = $db->prepare("SHOW TABLES LIKE $table_array"); $tableExists->execute(); if($tableExists->rowCount() > 0) { // do some code } else { echo "Unable to add because table does not exists"; } ``` UPDATE: Per suggestions below, I now do the following: ``` $tableExists = $db->prepare("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?"); $tableExists->execute(array($table_array)); if(!is_null($tableExist)) { //do something } else { echo "table does not exist; } ``` However, the if statement does not seem to work to determine whether the table exists or not. What else could I do?