Organizational Research By

Surprising Reserch Topic

check if mysql table exists or not using -'php,mysql,table,exists'


check if mysql table exists or not  using -'php,mysql,table,exists'


  Possible Duplicate:
  MySQL check if a table exists without throwing an exception  




I have a dynamic mysql query builder in my project that creates select queries from different tables.
I need to check if the current processing table exists or not.
Imagine that my tables are table1, table2, and table3. My code is something like this:  

<?php
for($i = 1 ; $i <= 3 ; $i++) {
   $this_table = 'table'.$i;
   $query = mysql_query("SELECT * FROM $this_table");
   // ...
}
?>


How can I do this check (Please tell me the simplest way).
    

asked Sep 8, 2015 by rajesh
0 votes
10 views



Related Hot Questions

5 Answers

0 votes
if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))==1) echo "Table exists"; else echo "Table does not exist"; Referenced from the PHP docs.
answered Sep 8, 2015 by rajesh
0 votes
$query = mysqli_query('SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME IN ("table1","table2","table3") AND TABLE_SCHEMA="yourschema"'); $tablesExists = array(); while( null!==($row=mysqli_fetch_row($query)) ){ $tablesExists[] = $row[0]; }
answered Sep 8, 2015 by rajesh
0 votes
$result = mysql_query("SHOW TABLES FROM $dbname"); while($row = mysql_fetch_row($result)) { $arr[] = $row[0]; } if(in_array($table,$arr)) { echo 'Table exists'; }
answered Sep 8, 2015 by rajesh
0 votes
You can try this $query = mysql_query("SELECT * FROM $this_table") or die (mysql_error()); or this $query = mysql_query("SELECT * FROM $this_table") or die ("Table does not exists!"); or this $query = mysql_query("SELECT * FROM $this_table"); if(!$query) echo "The ".$this_table." does not exists"; Hope it helps!
answered Sep 8, 2015 by rajesh
0 votes
MySQL way: SHOW TABLES LIKE 'pattern'; There's also a deprecated PHP function for listing all db tables, take a look at http://php.net/manual/en/function.mysql-list-tables.php Checkout that link, there are plenty of useful insight on the comments over there.
answered Sep 8, 2015 by rajesh

...