检查表是否存在

时间:2015-04-27 11:11:52

标签: php mysqli

我正在尝试检查我的数据库中是否存在表,但我总是得到“你在!”作为输出。 if语句的输出未显示在浏览器中。

我感谢任何帮助。

  $con = new mysqli ( 'localhost', 'root', '', 'bustracker' );
    echo "You are in!";
             if($con->connect_errno){
        die ( "connection problem!".$con->connect_error);
             }

          //check whether route's table exists.
          $results = $con->query("SHOW TABLES LIKE '".$route."'");

           if( ($results->num_rows) == 1){
             echo "Table exist";
           }else{
            echo "table does not exist";
           } 

2 个答案:

答案 0 :(得分:3)

SELECT 1 FROM tablename LIMIT 1;

如果没有错误,则表格存在。

或者,如果您想要正确,请使用INFORMATION_SCHEMA。

SELECT * 
FROM information_schema.tables
WHERE table_schema = 'yourdb' 
AND table_name = 'your_table_name'
LIMIT 1;

或者,您可以使用SHOW TABLES

SHOW TABLES LIKE 'your_table_name';

如果结果集中有一行,则表存在

答案 1 :(得分:-3)

Try this code it will definitely     work. simply copy and paste it. 

<?php 
    $con = @new mysqli('localhost', 'root', '', 'bustracker' );
    echo "You are in!";
    if ($con->connect_errno) {

        die ( "connection problem!".$con->connect_error);

             }
           else
           {
           $results = $con->query("SHOW TABLES LIKE '".$route."'");

           if( ($results->num_rows) == 1)
           {
             echo "Table exist";
           }
           else{
            echo "table does not exist";
           } 
           }
    ?>