Good Day, The task is to check the availability of a table in the database as simple as possible and in no case loading the process! I googled but on the forums except

SELECT * from 'table_name' 

nothing really can advise either absolutely huge constructions in 10 lines, there is really no simple command like php in php

 $yes = estlitablicav($database,$tablename); if ($yes == FALSE) { echo "Такой таблицы тут отродясь не было!"; } 

if there is no such option, then I will use things from the forms, thanks in advance!

  • I mean whether there is a built-in check function with sql or php! I can do this myself function mysql_table_seek ($ tablename, $ dbname) {$ table_list = mysql_query ("SHOW TABLES FROM ".$dbname." "); while ($ row = mysql_fetch_row ($ table_list)) {if ($ tablename == $ row [0]) {return true; }} return false; } - dantelol 5:56 pm
  • Hello. Maybe you can use my answer, which I left below. If not, write in the comments, I will delete it. - Yuri Svetlov

6 answers 6

Sampling from the table can lead to erroneous triggers if the table is empty.

My choice is SHOW TABLES with the mandatory use of preparation (prepare) of queries in order to avoid sql-injections, if the original table name is stored in a variable. Pseudocode, possible typos - not tested, just writing logic.

 function isTableExists($dbLink, $tableName){ if ($stmt = mysqli_prepare($dbLink, "SHOW TABLES LIKE '?';")) { mysqli_stmt_bind_param($stmt, "s", $tableName); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $result); mysqli_stmt_fetch($stmt); mysqli_stmt_close($stmt); } return ( mysqli_affected_rows($dbLink) > 0); } 

The fact that such queries usually take many lines, in my opinion, is connected with the problems caused by possible sql-injections. If you do not know what it is, just tamp on Google.

    Why do a sample when the list of tables is available.

     SHOW TABLES FROM 'db_name' LIKE 'нужная_таблица'; 

    In PHP, you can check it like this:

     $query = mysql_query("SHOW TABLES FROM 'db_name' LIKE 'нужная_таблица';"); $result = mysql_fetch_array($query); 

    And then process the results.

      Are you looking at any wrong forums?

       SELECT * FROM table LIMIT 1; -- тоже не самое хорошее решение. 

      Sample from information_schema or

       SHOW TABLES FROM `db_name` like 'table_name'; 
      • Yes, many people advised only until I figured out how to filter the returned parameter, then I run it so $ex = mysql_query("SHOW TABLES FROM literuture` LIKE 'imgcheck' "); RESSRS and this Resource is a special variable containing a link to an external resource. at the same time he returns both in the presence of a table and in the absence - dantelol
      • I can not find the documentation for SHOW TABLES FROM here - dantelol
      • filter through mysql_fetch_assoc - retvizan

      Just need to check that the next query returns a single line.

       SHOW TABLES LIKE 'table_name' 

        Check the table and do not understand the existence

        Example:

         CHECK TABLE tablename 

          In my opinion the best method is

           $exs = mysql_query("SELECT * FROM table LIMIT 1"); 

          simple select, and LIMIT - will not allow you to load the server too much, if anyone finds it better and most importantly, I think it’s easier to answer here, but for now I’ll continue to use such a design until there is an alternative