如何在以下语句中添加 Where 子句?
How to add Where clause in the following statement?
如何使用 CI 在以下查询中添加 where 子句?例如。其中名称 = 'Joe'
<?php
include 'dbclass.php';
$db = new DB();
$users = $db->getRows('users',array('order_by'=>'id DESC'));
if(!empty($users)):
$count = 0;
foreach($users as $user):
$count++;
?>
dbclass.php
片段:
public function getRows($table,$conditions = array()){
$sql = 'SELECT ';
$sql .= array_key_exists("select",$conditions)?$conditions['select']:'*';
$sql .= ' FROM '.$table;
if(array_key_exists("where",$conditions)){
$sql .= ' WHERE ';
$i = 0;
foreach($conditions['where'] as $key => $value){
$pre = ($i > 0)?' AND ':'';
$sql .= $pre.$key." = '".$value."'";
$i++;
}
}
if(array_key_exists("order_by",$conditions)){
$sql .= ' ORDER BY '.$conditions['order_by'];
}
if(array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
$sql .= ' LIMIT '.$conditions['start'].','.$conditions['limit'];
}elseif(!array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
$sql .= ' LIMIT '.$conditions['limit'];
}
$result = $this->db->query($sql);
我需要帮助来传递 Where 子句条件。
我正在使用 Codeignitor 4.
不确定 codeigniter 部分在哪里,看起来像您编写的代码,所以只需将 where
键添加到您传递的数组中,就像这样
$users = $db->getRows('users',array('where' => ["name => 'Joe']",
'order_by'=>'id DESC'));
如何使用 CI 在以下查询中添加 where 子句?例如。其中名称 = 'Joe'
<?php
include 'dbclass.php';
$db = new DB();
$users = $db->getRows('users',array('order_by'=>'id DESC'));
if(!empty($users)):
$count = 0;
foreach($users as $user):
$count++;
?>
dbclass.php 片段:
public function getRows($table,$conditions = array()){
$sql = 'SELECT ';
$sql .= array_key_exists("select",$conditions)?$conditions['select']:'*';
$sql .= ' FROM '.$table;
if(array_key_exists("where",$conditions)){
$sql .= ' WHERE ';
$i = 0;
foreach($conditions['where'] as $key => $value){
$pre = ($i > 0)?' AND ':'';
$sql .= $pre.$key." = '".$value."'";
$i++;
}
}
if(array_key_exists("order_by",$conditions)){
$sql .= ' ORDER BY '.$conditions['order_by'];
}
if(array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
$sql .= ' LIMIT '.$conditions['start'].','.$conditions['limit'];
}elseif(!array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
$sql .= ' LIMIT '.$conditions['limit'];
}
$result = $this->db->query($sql);
我需要帮助来传递 Where 子句条件。 我正在使用 Codeignitor 4.
不确定 codeigniter 部分在哪里,看起来像您编写的代码,所以只需将 where
键添加到您传递的数组中,就像这样
$users = $db->getRows('users',array('where' => ["name => 'Joe']",
'order_by'=>'id DESC'));