How to check an empty table already in the database

I am creating a site admin panel. When the administrator enters a category, a table with the same category name is created in the database, the fields are still the same. when running admin enter many categories. Then how can I check that the table has already been created in the database .. because all tables are empty. for this

I am trying to use 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 this always shows "no table" if the table is in db .. how can i solve this.

+3


source to share


2 answers


Don't create a separate table for each category!
Make it one , with the whole set of fields and with the category name (or category ID, depending on the database schema).

These are the very basics of how databases work.



Thus, you just need to run a regular query to check if such a category name exists:

SELECT id FROM categories WHERE name = 'name to check';

      

+3


source


Take a look at the diagram INFORMATION_SCHEMA

, in particular the TABLES

table
.

IE:



SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = `admin_table_name`;

      

-1


source







All Articles