英文:
Error Number: 1064 CodeIgniter query builder
问题
以下是您提供的代码的翻译部分:
在您的SQL语法中存在错误;请检查与您的MariaDB服务器版本相对应的手册,以查找在第6行附近使用正确语法的方法
SELECT
`instrument`.`id` as `id`,
`instrument`.`name` as `name`,
`price`,
`isix`,
`wkn`,
`isin`,
`instrument`.`account` as `accID`,
`own_invest`,
`interest`,
(select company
from chart_of_accounts
where chart_of_accounts.id = accID
group by company
) as company_id,
(select url
from company
where company.id = company_id
) as company_url,
`tiv_bond_type`.`name` as `bond_type`
FROM
`instrument`
LEFT JOIN `bond` ON `bond`.`instrument` = `instrument`.`id`
LEFT JOIN `tiv_bond_type` ON `tiv_bond_type`.`bond_type` = `bond`.`type`
WHERE
( ) AND
`instrument`.`type` = 2
ORDER BY
`instrument`.`price` DESC LIMIT 100
文件名:models/Search_model.php
行号:251
public function search_bonds($name, $isin, $isix, $wkn, $type, $initial_price, $end_price, $initial_interest, $end_interest) {
$bond_name = explode(" ", $name);
$this->db->select("instrument.id as id, instrument.name as name, price, isix, wkn, isin, instrument.account as accID, own_invest, interest,
(select company from chart_of_accounts where chart_of_accounts.id = accID group by company) as company_id,
(select url from company where company.id = company_id) as company_url,
tiv_bond_type.name as bond_type");
$this->db->from("instrument");
$this->db->join("bond", "bond.instrument = instrument.id", "left", "outer");
$this->db->join("tiv_bond_type", "tiv_bond_type.bond_type = bond.type", "left");
if (strlen($isin) > 3)
$this->db->where("instrument.isin", $isin);
if (strlen($name) > 1) {
$this->db->group_start();
$this->create_search_permutations($bond_name, 1);
$this->db->group_end();
}
if (strlen($isix))
$this->db->where("instrument.isix", $isix);
if (strlen($wkn) > 3 )
$this->db->where("instrument.wkn", $wkn);
if (strlen($type) && is_numeric($type))
$this->db->where('bond.type', $type);
if (is_numeric($initial_price) && is_numeric($end_price)) {
$this->db->group_start();
$this->db->where("instrument.price >=", $initial_price);
$this->db->where("instrument.price <=", $end_price);
$this->db->group_end();
}
else if (is_numeric($initial_price) && ! is_numeric($end_price))
$this->db->where("instrument.price >=", $initial_price);
else if (is_numeric($end_price) && ! is_numeric($initial_price))
$this->db->where("instrument.price <=", $end_price);
if (is_numeric($initial_interest) && is_numeric($end_interest)) {
$this->db->group_start();
$this->db->where("bond.interest >=", $initial_interest);
$this->db->where("bond.interest <=", $end_interest);
$this->db->group_end();
}
else if (is_numeric($initial_interest) && ! is_numeric($end_interest))
$this->db->where("bond.interest >=", $initial_interest);
else if (is_numeric($end_price) && ! is_numeric($initial_interest))
$this->db->where("bond.interest <=", $end_interest);
$this->db->where("instrument.type", 2);
$this->db->order_by("instrument.price", "desc");
$this->db->limit(100);
return $this->db->get()->result_array();
}
private function create_search_permutations($search_array, $table_name, $permutations = array()) {
/*
$query_type变量定义了查询字符串所针对的表列类型
即$query_type == 1定义查询字符串是为了instrument.name,$query_type == 2是为了company.name
*/
if (empty($search_array)) {
if ($table_name == 'instrument')
$this->db->or_where("instrument.name like '%" . join('%', $permutations) . "%'");
elseif ($table_name == 'company')
$this->db->or_where("company.name like '%" . join('%', $permutations) . "%'");
else
return;
}
else {
for ($iterator = count($search_array) - 1; $iterator >= 0; --$iterator) {
$new_search_array = $search_array;
$new_permutations = $permutations;
list($key) = array_splice($new_search_array, $iterator, 1);
array_unshift($new_permutations, $key);
$this->create_search_permutations($new_search_array, $table_name, $new_permutations);
}
}
}
希望这有助于您找出代码中的问题。
英文:
>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 ') AND instrument
.type
= 2 ORDER BY instrument
.price
DESC LIMIT 100' at line 6
SELECT
`instrument`.`id` as `id`,
`instrument`.`name` as `name`,
`price`,
`isix`,
`wkn`,
`isin`,
`instrument`.`account` as `accID`,
`own_invest`,
`interest`,
(select company
from chart_of_accounts
where chart_of_accounts.id = accID
group by company
) as company_id,
(select url
from company
where company.id = company_id
) as company_url,
`tiv_bond_type`.`name` as `bond_type`
FROM
`instrument`
LEFT JOIN `bond` ON `bond`.`instrument` = `instrument`.`id`
LEFT JOIN `tiv_bond_type` ON `tiv_bond_type`.`bond_type` = `bond`.`type`
WHERE
( ) AND
`instrument`.`type` = 2
ORDER BY
`instrument`.`price` DESC LIMIT 100
Filename: models/Search_model.php
Line Number: 251
public function search_bonds($name, $isin, $isix, $wkn, $type, $initial_price, $end_price, $initial_interest, $end_interest) {
$bond_name = explode(" ", $name);
$this->db->select("instrument.id as id, instrument.name as name, price, isix, wkn, isin, instrument.account as accID, own_invest, interest,
(select company from chart_of_accounts where chart_of_accounts.id = accID group by company) as company_id,
(select url from company where company.id = company_id) as company_url,
tiv_bond_type.name as bond_type");
$this->db->from("instrument");
$this->db->join("bond", "bond.instrument = instrument.id", "left", "outer");
$this->db->join("tiv_bond_type", "tiv_bond_type.bond_type = bond.type", "left");
if (strlen($isin) > 3)
$this->db->where("instrument.isin", $isin);
if (strlen($name) > 1) {
$this->db->group_start();
$this->create_search_permutations($bond_name, 1);
$this->db->group_end();
}
if (strlen($isix))
$this->db->where("instrument.isix", $isix);
if (strlen($wkn) > 3 )
$this->db->where("instrument.wkn", $wkn);
if (strlen($type) && is_numeric($type))
$this->db->where('bond.type', $type);
if (is_numeric($initial_price) && is_numeric($end_price)) {
$this->db->group_start();
$this->db->where("instrument.price >= ", $initial_price);
$this->db->where("instrument.price <= ", $end_price);
$this->db->group_end();
}
else if (is_numeric($initial_price) && ! is_numeric($end_price))
$this->db->where("instrument.price >= ", $initial_price);
else if (is_numeric($end_price) && ! is_numeric($initial_price))
$this->db->where("instrument.price <= ", $end_price);
if (is_numeric($initial_interest) && is_numeric($end_interest)) {
$this->db->group_start();
$this->db->where("bond.interest >= ", $initial_interest);
$this->db->where("bond.interest <= ", $end_interest);
$this->db->group_end();
}
else if (is_numeric($initial_interest) && ! is_numeric($end_interest))
$this->db->where("bond.interest >= ", $initial_interest);
else if (is_numeric($end_price) && ! is_numeric($initial_interest))
$this->db->where("bond.interest <= ", $end_interest);
$this->db->where("instrument.type", 2);
$this->db->order_by("instrument.price", "desc");
$this->db->limit(100);
return $this->db->get()->result_array();
}
private function create_search_permutations($search_array, $table_name, $permutations = array()) {
/*
$query_type variable is defining the type of table column the query string is targeting
ie $query_type == 1 defines that the query string is for instrument.name and $query_type == 2 is for company.name
*/
if (empty($search_array)) {
if ($table_name == 'instrument')
$this->db->or_where("instrument.name like '%".join('%', $permutations)."%'");
elseif ($table_name == 'company')
$this->db->or_where("company.name like '%".join('%', $permutations)."%'");
else
return;
}
else {
for ($iterator = count($search_array) - 1; $iterator >= 0; --$iterator) {
$new_search_array = $search_array;
$new_permutations = $permutations;
list($key) = array_splice($new_search_array, $iterator, 1);
array_unshift($new_permutations, $key);
$this->create_search_permutations($new_search_array, $table_name, $new_permutations);
}
}
}
Anyone to helping with figuring out what is wrong with my code here.
答案1
得分: 1
我的猜测是问题出在create_search_permutations
方法上。该方法被包裹在一个group_start
/group_end
对中,但实际上并没有对查询进行任何操作。
英文:
My guess would be that the problem is with the create_search_permutations
method. That method is wrapped in a group_start
/group_end
pair, but doesn't actually do anything to the query.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论