我的where子句有两个条件,我正在数组中编写它。以下是到目前为止的代码:
public function getRelatedNews($id, $news_id) {
$args = array(
'fields' => array(
'news.id',
'news.title',
'news.story',
'news.image',
'news.status',
'news.added_date',
'categories.title AS news_category',
'(SELECT users.full_name FROM users WHERE id = news.added_by) as author',
),
'where' => array(
'news_category' => $id
'news.id' !=> $news_id
),
'join' => 'LEFT JOIN categories on news.news_category = categories.id',
'limit' => array(0, 2)
);
return $this->select($args);
}
当我在数组中写入条件时,我将=>
用于相等,但对于第二个条件,我将无法使用!=>
。如果它们不像上面那样在数组中,我应该对不相等的条件使用什么?
我有SELECT
这样的查询:
final protected function select($args = array(), $is_die = false){
try {
$this->sql = "SELECT ";
if (isset($args['fields'])) {
if (is_array($args['fields'])) {
$this->sql .= implode(', ', $args['fields']);
} else {
$this->sql .= $args['fields'];
}
} else {
$this->sql .= " * ";
}
$this->sql .= " FROM ";
if (!isset($this->table) || empty($this->table)) {
throw new Exception("Table not set");
}
$this->sql .= $this->table;
/*Join Query*/
if (isset($args['join']) && !empty($args['join'])) {
$this->sql .= " ".$args['join'];
}
/*Join Query*/
if (isset($args['where']) && !empty($args['where'])) {
if (is_array($args['where'])) {
$temp = array();
foreach ($args['where'] as $column_name => $value) {
$str = $column_name." = :".$column_name;
$temp[] = $str;
}
$this->sql .= " WHERE ".implode(' AND ', $temp);
} else {
$this->sql .= " WHERE ".$args['where'];
}
}
/*Group*/
if (isset($args['group_by']) && !empty($args['group_by'])) {
$this->sql .= " GROUP BY ".$args['group_by'];
}
/*Group*/
/*Order*/
if (isset($args['order_by']) && !empty($args['order_by'])) {
$this->sql .= " ORDER BY ".$args['order_by'];
} else {
$this->sql .= " ORDER BY ".$this->table.".id DESC";
}
/*Order*/
/*Limit*/
if (isset($args['limit']) && !empty($args['limit'])) {
if (is_array($args['limit'])) {
$this->sql .= " LIMIT ".$args['limit'][0].",".$args['limit'][1];
} else {
$this->sql .= " LIMIT ".$args['limit'];
}
}
/*Limit*/
$this->stmt = $this->conn->prepare($this->sql);
if (isset($args['where']) && !empty($args['where']) && is_array($args['where'])) {
foreach ($args['where'] as $column_name => $value) {
if (is_int($value)) {
$param = PDO::PARAM_INT;
} elseif (is_bool($value)) {
$param = PDO::PARAM_BOOL;
} elseif (is_null($value)) {
$param = PDO::PARAM_NULL;
} else {
$param = PDO::PARAM_STR;
}
if ($param) {
$this->stmt->bindValue(":".$column_name, $value, $param);
}
}
}
if ($is_die) {
echo $this->sql;
debugger($this->stmt);
}
$this->stmt->execute();
$data = $this->stmt->fetchAll(PDO::FETCH_OBJ);
return $data;
} catch (PDOException $e) {
error_log(
date('Y-m-d h:i:s A').", Select Query: ".$e->getMessage()."\r\n"
, 3, ERROR_PATH.'/error.log');
return false;
} catch (Exception $e) {
error_log(
date('Y-m-d h:i:s A').", General: ".$e->getMessage()."\r\n"
, 3, ERROR_PATH.'/error.log');
return false;
}
}
答案 0 :(得分:3)
要反映我的评论,请将您的数据结构更改为此:
public function getRelatedNews($id, $news_id) {
$args = array(
'fields' => array(
'news.id',
'news.title',
'news.story',
'news.image',
'news.status',
'news.added_date',
'categories.title AS news_category',
'(SELECT users.full_name FROM users WHERE id = news.added_by) as author',
),
'where' => array(
'news_category' => $id
'news.id' => array('value' => $news_id, 'operator' => '!=', ),
),
'join' => 'LEFT JOIN categories on news.news_category = categories.id',
'limit' => array(0, 2)
);
return $this->select($args);
}
现在,您仍然必须根据新数据更改select
方法。
foreach ($args['where'] as $column_name => $data) {
/**
Transform to array when only value is passed
Use default operator = when this is the case
**/
if (!is_array($data)) {
$data = array(
'value' => $data,
'operator' => '=',
);
}
$str = $column_name.' '.$data['operator'].' :'.str_replace('.', '_', $column_name);
$temp[] = $str;
}
并更改值绑定:
foreach ($args['where'] as $column_name => $data) {
$value = is_array($data) ? $data['value'] : $data; //check if passed where statement was an array, fetch value if so
if (is_int($value)) {
$param = PDO::PARAM_INT;
}elseif (is_bool($value)) {
$param = PDO::PARAM_BOOL;
}elseif (is_null($value)) {
$param = PDO::PARAM_NULL;
}else {
$param = PDO::PARAM_STR;
}
if ($param) {
$this->stmt->bindValue(":".str_replace('.', '_', $column_name), $value, $param);
}
}
答案 1 :(得分:-1)
您需要像这样使用您的助手
public function getRelatedNews($id, $news_id) {
$args = array(
'fields' => array(
'news.id',
'news.title',
'news.story',
'news.image',
'news.status',
'news.added_date',
'categories.title AS news_category',
'(SELECT users.full_name FROM users WHERE id = news.added_by) as author',
),
'where' => array(
'news_category' => $id
'news.id !=' => $news_id
),
'join' => 'LEFT JOIN categories on news.news_category = categories.id',
'limit' => array(0, 2)
);
return $this->select($args);
}
答案 2 :(得分:-3)
您当前有!=>,如果您不希望等于==