[英]How can I use select with where condition in CodeIgniter?
In CodeIgniter while using where statement I have following error 在CodeIgniter中使用where语句时出现以下错误
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
And my statement is : 我的声明是:
$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;
}
if I have remove 如果我删除了
$this->db->where('cat_type',1);
code is running perfect. 代码运行完美。 How can I use where condition ?
如何使用条件?
Try below code it will help you to sort your problem 试试下面的代码,它将帮助您解决问题
$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;
Hope this will help... 希望这会有所帮助...
其他方式 :
query1 = $this->db->query("SELECT * FROM games WHERE cat_type = 1")->result_array();
Use Codeigniter Active record (modified PHP Query to prevent SQL Injection) 使用Codeigniter Active记录 (修改的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
}
}
Try 尝试
$this->db->select('*');
$this->db->where('cat_type',1);
$query = $this->db->get('games');
This will work perfectly 这将完美地工作
Let me know if you require any further help 让我知道您是否需要任何进一步的帮助
Try this Code 试试这个代码
$this->db->select('*')->from('games');
$this->db->where('cat_type=1');
$query = $this->db->get();
return $query->result();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.