gpt4 book ai didi

php - Codeigniter 数据库查询告诉错误+格式

转载 作者:行者123 更新时间:2023-11-29 18:24:13 24 4
gpt4 key购买 nike

我选择了一个表,但在错误部分显示我选择了另一张表。我不知道为什么会发生这种情况。我在某些 View 中进行查询,但没有问题。这个问题并不存在于所有地方.但是在一些 View 和 Controller 中。

这是我的 MY_Model

protected function from()
{
return $this->db->from($this->_table_name);
}

protected function where()
{
if(is_array($this->_where))
{
foreach($this->_where as $where)
{
return $this->db->where($where['field_name'],$where['primary_key']);
}
}
else
{
return $this->db->where($this->_field_name,$this->_primary_key);
}
}

protected function or_where()
{
if(is_array($this->_where))
{
foreach($this->_where as $where)
{
return $this->db->or_where($where['field_name'],$where['primary_key']);
}
}
else
{
$this->db->or_where($this->_or_field_name,$this->_or_primary_key);
}
}

protected function join()
{
if(is_array($this->_join))
{
foreach($this->_join as $join)
{
return $this->db->join($join['table'],$join['query']);
}
}
else
{
return 'Invalid Query';
}
}

protected function order()
{
//print_r($this->_order);exit();
if(is_array($this->_order))
{
return $this->db->order_by($this->_order['by'],$this->_order['order']);
}
else
{
return "Invalid Query";
}
}

protected function limit()
{
if(is_array($this->_limit))
{
foreach($this->_limit as $limit)
{
return $this->db->limit($limit['from'],$limit['to']);
}
}
else
{
return $this->db->limit($this->_limit);
}
}

public function get()
{
$this->db->select('*');
if($this->_table_name)
{
$this->from();
}
if($this->_where || ($this->_field_name && $this->_primary_key))
{
$this->where();
}
if($this->_or_where || ($this->_or_field_name && $this->_or_primary_key))
{
$this->or_where();
}
if($this->_join)
{
$this->join();
}
if($this->_order)
{
$this->order();
}
if($this->_limit)
{
$this->limit();
}

return $this->db->get();
}

这是我的查询

    $this->cm->_table_name = "products";
$this->cm->_field_name = "products.productID";
$this->cm->_primary_key = $productID;
$this->cm->_join = array
(
array
(
'table' => 'product_details',
'query' => 'products.productID=product_details.productID',
'type' => 'left',
),
);
$data['product'] = $this->cm->get();

这是错误

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '* FROM 'products', 'parentcategory' WHERE 'products'.'productID' = '30'' at line 1

SELECT *, * FROM `products`, `parentcategory` WHERE `products`.`productID` = '30'

Filename: C:/xampp/htdocs/mv/system/database/DB_driver.php

Line Number: 691

我不知道为什么会发生这种情况,请帮助我。

最佳答案

更改您的查询如下:

SELECT products.*, parentcategory.* FROM `products`, `parentcategory` WHERE `products`.`productID` = '30'

SELECT * FROM `products`, `parentcategory` WHERE `products`.`productID` = '30'

您可以添加另一个参数,例如:

$this->cm->_field_to_select = "products.*,parentcategory.*";

并将其添加到您的 get 方法中

if ($this->_field_to_select) {
$this->db->select($this->_field_to_select);
}
else {
$this->db->select('*');
}

关于php - Codeigniter 数据库查询告诉错误+格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46320465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com