簡體   English   中英

檢查MySQL中是否存在表

[英]Check if a table exists in mysql

我正在嘗試在codeigniter中使用mysql檢查表是否存在以及該表中是否有記錄。 這是我嘗試過的功能,但id不起作用。

function isRowExist($table, $id)
{
    if(mysqli_query("DESCRIBE {$table}")) {
        $query = $this->db->query("SELECT * FROM {$table} WHERE id = '{$id}'");
    }
    return $query->num_rows();
}

任何幫助,將不勝感激。

您可以嘗試使用此codeigniter函數來檢查表是否已存在。

   if ($this->db->table_exists('tbl_user')) {
        // table exists (Your query)
    } else {
        // table does not exist (Create table query)          
    }

您可以通過此功能檢查表是否存在:

if ($result = $mysqli->query("SHOW TABLES LIKE '".$table."'")) {
    if($result->num_rows == 1) {
        echo "Table exists";
    }
}
else {
    echo "Table does not exist";
}

使用此代碼檢查表在codeigniter中是否存在

$this->db->table_exists();

您可以將其與條件語句一起使用。

if ($this->db->table_exists('table_name'))
{
     // some code...
} else {
     // not exist
}

此代碼可以幫助:

include 'connection.php';
function createSignupTable()
{
  $conn = $GLOBALS['conn'];
  $error = array('message1' =>'Table created successfuly' , 'message2'=>'Problem creating the table');
  if($conn == true)
  {
    $result = $conn->query("SHOW TABLES LIKE 'signuptable'");
    if($result->num_rows == 1){
        echo "table exists";
    }else{  
        $create_table1 = 'CREATE TABLE signuptable(
            cs_user_id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
            firstname VARCHAR(200) NOT NULL,
            lastname VARCHAR(200) NOT NULL,
            username VARCHAR(200) UNIQUE KEY NOT NULL,
            AKA VARCHAR(200) UNIQUE KEY NOT NULL,
            password VARCHAR(200) NOT NULL,
            email VARCHAR(200) NOT NULL,
            phone_number VARCHAR(200) NOT NULL,
            Date_signed_up TIMESTAMP
        )';
        $query_1 = $conn->query($create_table1);
        if($query_1 == true){
            echo $error["message1"];
        }else{
            die($conn->error);
        }
    }
  }
}
createSignupTable();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM