2017-06-29 79 views
1

CI中同時使用WHERE語句我有以下錯誤如何在CodeIgniter中的where條件中使用select?

A PHP Error was encountered 

Severity: Warning 

Message: preg_match(): Compilation failed: a numbered reference must not be zero at offset 34 

Filename: database/DB_driver.php 

Line Number: 1543 

Backtrace: 

File: D:\xampp\htdocs\manojgame\application\models\Main_model.php 
Line: 34 
Function: where 

File: D:\xampp\htdocs\manojgame\application\controllers\Games.php 
Line: 21 
Function: select_data_bytitle 

File: D:\xampp\htdocs\manojgame\index.php 
Line: 262 
Function: require_once 

我的說法是:

$this->db->select('*'); 

$this->db->from('games'); 

$this->db->where('cat_type',1); 

$query = $this->db->get(); 

if ($query->num_rows() > 0) { 
    $row = $query->row_array(); 
    return $row; 
} 

,如果我有刪除

$this->db->where('cat_type',1); 

代碼運行完美。 如何使用條件?下面的代碼

回答

0

嘗試它會幫助你理清你的問題

$this->db->where('games',1); //----> you can pass variable instead of 1 
    $query = $this->db->get('games'); 
    if($query->num_rows() > 0) 
    { 
     return $query->row_array(); 
    } 
    return false; 

希望這將有助於...

0

另一種方式:

query1 = $this->db->query("SELECT * FROM games WHERE cat_type = 1")->result_array(); 
0

使用笨Active record(修改PHP查詢防止SQL注入)

//EXAMPLE: SELECT * FROM games WHERE cat_type = 1 
$query = $this->db->select('*')->get_where('games', array('cat_type' => '1')); 

//EXAMPLE with condition: SELECT * FROM games WHERE cat_type != 1 
/* 
$query = $this->db->select('*')->get_where('games', array('cat_type !=' => '1')); 
*/ 

if($query->result()){ 
foreach ($query->result() as $row) { 
#do something here to get value of another rows or etc 
} 
} 
0

嘗試

$this->db->select('*'); 
$this->db->where('cat_type',1); 
$query = $this->db->get('games'); 

這將很好地工作

讓我知道如果您需要任何進一步的幫助

0

試試這個代碼

$this->db->select('*')->from('games'); 
    $this->db->where('cat_type=1'); 
     $query = $this->db->get(); 

     return $query->result(); 
相關問題