代码之家  ›  专栏  ›  技术社区  ›  clops

MySQL检查表是否存在而不引发异常

  •  121
  • clops  · 技术社区  · 17 年前

    检查mysql中是否存在表(最好是通过php中的pdo)而不引发异常的最佳方法是什么?我不想分析“show tables like”等的结果。一定有某种布尔查询?

    10 回复  |  直到 9 年前
        1
  •  198
  •   nickf    17 年前

    我不知道它的PDO语法,但这看起来非常直接:

    $result = mysql_query("SHOW TABLES LIKE 'myTable'");
    $tableExists = mysql_num_rows($result) > 0;
    
        2
  •  39
  •   Michael Todd    17 年前

    如果您使用的是MySQL5.0及更高版本,您可以尝试:

    SELECT COUNT(*)
    FROM information_schema.tables 
    WHERE table_schema = '[database name]' 
    AND table_name = '[table name]';
    

    任何结果都表明该表存在。

    来自: http://www.electrictoolbox.com/check-if-mysql-table-exists/

        3
  •  8
  •   Falk    9 年前

    我使用mysqli创建了以下函数。假设您有一个名为$con的mysqli实例。

    function table_exist($table){
        global $con;
        $table = $con->real_escape_string($table);
        $sql = "show tables like '".$table."'";
        $res = $con->query($sql);
        return ($res->num_rows > 0);
    }
    

    希望它有帮助。

    警告: 正如@jcaron建议的那样,此函数可能容易受到sqlinjection-attacs的攻击,因此请确保 $table var是干净的,甚至更好地使用参数化查询。

        4
  •  3
  •   erandac    12 年前

    下面是我在使用存储过程时喜欢的解决方案。用于检查当前数据库中是否存在该表的自定义mysql函数。

    delimiter $$
    
    CREATE FUNCTION TABLE_EXISTS(_table_name VARCHAR(45))
    RETURNS BOOLEAN
    DETERMINISTIC READS SQL DATA
    BEGIN
        DECLARE _exists  TINYINT(1) DEFAULT 0;
    
        SELECT COUNT(*) INTO _exists
        FROM information_schema.tables 
        WHERE table_schema =  DATABASE()
        AND table_name =  _table_name;
    
        RETURN _exists;
    
    END$$
    
    SELECT TABLE_EXISTS('you_table_name') as _exists
    
        5
  •  3
  •   Esoterica    12 年前

    如果有人来找这个问题的话,这个帖子就会简单地贴出来。即使有人回答了一点。一些回复使它比需要的更复杂。

    对于mysql*我使用:

    if (mysqli_num_rows(
        mysqli_query(
                        $con,"SHOW TABLES LIKE '" . $table . "'")
                    ) > 0
            or die ("No table set")
        ){
    

    在PDO中,我使用:

    if ($con->query(
                       "SHOW TABLES LIKE '" . $table . "'"
                   )->rowCount() > 0
            or die("No table set")
       ){
    

    有了这个,我就把其他条件推到或中。为了我的需要,我只需要死。尽管你可以设置或其他东西。有些人可能更喜欢if/else if/else。然后移除或提供if/else if/else。

        6
  •  2
  •   John Green    10 年前

    由于“显示表”在较大的数据库上可能比较慢,因此我建议使用“描述”并检查结果是否为真/假。

    $tableExists = mysqli_query("DESCRIBE `myTable`");
    
        7
  •  -1
  •   Namkeen Butter    13 年前
    $q = "SHOW TABLES";
    $res = mysql_query($q, $con);
    if ($res)
    while ( $row = mysql_fetch_array($res, MYSQL_ASSOC) )
    {
        foreach( $row as $key => $value )
        {
            if ( $value = BTABLE )  // BTABLE IS A DEFINED NAME OF TABLE
                echo "exist";
            else
                echo "not exist";
        }
    }
    
        8
  •  -1
  •   gilcierweb    10 年前

    ZED框架

    public function verifyTablesExists($tablesName)
        {
            $db = $this->getDefaultAdapter();
            $config_db = $db->getConfig();
    
            $sql = "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '{$config_db['dbname']}'  AND table_name = '{$tablesName}'";
    
            $result = $db->fetchRow($sql);
            return $result;
    
        }
    
        9
  •  -1
  •   Community Mohan Dere    9 年前

    如果希望这样做的原因是创建有条件的表,那么“如果不存在则创建表”似乎是该作业的理想选择。直到我发现这一点,我才使用上面的“描述”方法。更多信息在这里: MySQL "CREATE TABLE IF NOT EXISTS" -> Error 1050

        10
  •  -9
  •   RocketDonkey    13 年前

    你为什么这么难理解?

    function table_exist($table){ 
        $pTableExist = mysql_query("show tables like '".$table."'");
        if ($rTableExist = mysql_fetch_array($pTableExist)) {
            return "Yes";
        }else{
            return "No";
        }
    }