3

i am creating a admin panel of a site.when admin enter a category , a table of same category name is create in database, fields are all table are same.. in starting admin enter a lot of category..then how can i check that a table is already created in database..because all tables are empty. for that

i am trying this code

     <?php $con = mysql_connect("localhost","root","");
     if (!$con)
     {die('Could not connect: ' . mysql_error());}
   $sql="SELECT * FROM admin";//(for trial im a changing the name manuaaly)

    $result=@mysql_query($sql);

  if (!$result)

      {
        echo "No table exists";
     }
      else
      {
     echo "yes";
     }
    ?>

but in this its always show "no table exists", if table is in the db.. how can i solve this..

omnath
  • 87
  • 1
  • 9
  • http://stackoverflow.com/questions/9008299/check-if-mysql-table-exists-or-not – kufudo Feb 17 '13 at 04:58
  • 1
    Please, don't use `mysql_*` functions for new code. They are [deprecated](http://php.net/manual/en/intro.mysql.php). Use [prepared statements](http://goo.gl/vn8zQ) with either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). – peterm Feb 17 '13 at 05:00
  • your logic is fundamentally flawed. an empty table CAN and DOES exist. checking for the presence of rows in a table means nothing. if the table doesn't exist, you'd get a query failure (e.g. mysql_query returning false and error code 1146, not "no rows found" – Marc B Feb 17 '13 at 05:08
  • but my client is entering 200 tables by category so that he want to clear that these tables are entered before..so that i am asking this.. – omnath Feb 17 '13 at 05:10

2 Answers2

3

Do not create a separate table for each category!
Make it one, with all the set of fields and one with category name (or a category id, depends on the database schema).

That's the very basics of how databases works.

Thus you will have to just run a regular query to check if such category name already exists:

SELECT id FROM categories WHERE name = 'name to check';
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • yes sir,i can use SELECT id FROM categories WHERE name = 'name to check'; this method..but save all records in single table..then all records are going more then 10000..then in that case is it effect on mysql query.. – omnath Feb 17 '13 at 05:16
-1

Take a look at the INFORMATION_SCHEMA schema, specifically the TABLES table.

IE:

SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = `admin_table_name`;
Mitch
  • 21,223
  • 6
  • 63
  • 86