简体   繁体   English

如何在CodeIgniter中将select与where条件一起使用?

[英]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.

相关问题 选择并在codeigniter中的条件 - select and where condition in codeigniter 如何在Codeigniter中显示或查看多个复选框的WHERE条件 - How can I display or view multiple checkbox WHERE condition in Codeigniter 如何在ifigniter中使用if条件和条件以及where条件编写选择查询 - how to write select query using if condition,And condition and where condition in codeigniter 我如何使用 mysqli 到 SELECT 以及根据 WHERE 条件删除和返回单行 - How can I use mysqli to SELECT and both DELETE and RETURN a single row based on WHERE condition 我们如何在codeigniter中将or_where_in与其他where条件一起使用 - how can we use or_where_in along with other where condition in codeigniter 如何在where子句[Codeigniter]中使用连接 - How can I use concatenation in where clause [Codeigniter] 如何在Codeigniter中使用多个where子句? - how can i use multiple where clause In codeigniter? 当我在基于条件的查询中使用if else条件时,如何在PHP CODEIGNITER中的视图上获取该值 - When i use if else condition in query based on condition, how can i get that value on view in PHP CODEIGNITER 如何在Codeigniter中将set_select与Ajax结合使用 - How can I use set_select with Ajax in Codeigniter 我们如何使用array_merge中的where条件导致codeigniter - How can we use where condition in array_merge result in codeigniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM