【问题标题】:Check if a table exists in mysql检查mysql中是否存在表
【发布时间】:2017-02-22 06:00:45
【问题描述】:

我正在尝试使用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();
}

任何帮助将不胜感激。

【问题讨论】:

标签: php mysql codeigniter


【解决方案1】:

你可以试试这个codeigniter函数来检查表是否已经存在。

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

【讨论】:

    【解决方案2】:

    使用此代码检查 codeigniter 中的表是否存在

    $this->db->table_exists();
    

    您可以将其与条件语句一起使用。

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

    【讨论】:

      【解决方案3】:

      您可以通过此功能检查表是否存在:

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

      【讨论】:

        【解决方案4】:

        此代码可能会有所帮助:

        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();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-02-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-06
          • 2010-12-21
          • 1970-01-01
          • 2016-12-05
          相关资源
          最近更新 更多