2017-02-22 1 views
0

테이블이 존재하는지 확인하고, 레코드가 있으면 테이블에서 codeigniter에 mysql을 사용합니다. 여기에 시도한 기능이 있지만 ID가 작동하지 않습니다.mysql에 테이블이 존재하는지 확인하십시오.

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

어떤 도움을 주시면 감사하겠습니다.

+1

의 사용 가능한 복제 [MySQL이 - 테이블 "선택"사용하지 않고있는 경우 확인] (http://stackoverflow.com/questions/8829102/mysql- check-if-table-exists-without-using-select-from) – jitendrapurohit

+0

테이블이 정의되지 않은 경우 루프에 들어 가지 않고 정의되지 않은 쿼리 오류가 발생합니다. 이드 때문에 실패합니다. –

답변

2

이 codeigniter 기능을 사용하여 이미 테이블을 점검 할 수 있습니다.

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

표는이 함수가 존재하는지 여부를 확인할 수 있습니다

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

사용이 코드 표를 확인하기 위해 존재 여부를 당신은 조건문과 함께 이것을 사용할 수 있습니다

$this->db->table_exists(); 

CodeIgniter의

있다.

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

이 코드는 데 도움이 될 수

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(); 
관련 문제