简体   繁体   English

检查表是否存在

[英]check whether table exist

I am trying to check whether a table exists in my database but I am always getting "You are in!"我试图检查我的数据库中是否存在一个表,但我总是收到“你在!” as output.作为输出。 The output of the if statement is not being displayed in the browser. if 语句的输出未显示在浏览器中。

I appreciate any help.我很感激任何帮助。

  $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";
           } 
SELECT 1 FROM tablename LIMIT 1;

If there's no error, table exists.如果没有错误,则表存在。

Or, if you want to be correct, use INFORMATION_SCHEMA.或者,如果您想要正确,请使用 INFORMATION_SCHEMA。

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

Alternatively, you can use SHOW TABLES或者,您可以使用 SHOW TABLES

SHOW TABLES LIKE 'your_table_name';

If there is a row in the resultset, table exists如果结果集中有一行,则表存在

<?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";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM