【发布时间】:2015-07-24 20:02:32
【问题描述】:
我正在尝试从名为“post_id”的数据库中获取一个属性值,以便在其他地方使用。
这是我的型号代码。
$copy_query = "SELECT post_id FROM posts order by post_id DESC limit 1"; //query for selecting last post's post_id
$result = $this->db->query($copy_query); //adding that post_id to the $result variable
$sub_data = array(
'study_education_level' => $this->input->post('sub_education_level'),
'tourism_country' => $this->input->post('sub_tourism_country'),
'tourism_place_name' => $this->input->post('sub_tourism_placeName'),
'post_id' => $result
);
$this->db->insert('subcategories',$sub_data);
但是每当我运行代码时,它都会给我两个错误。
-
遇到了 PHP 错误
Severity: 4096 Message: Object of class CI_DB_mysql_result could not be converted to string Filename: mysql/mysql_driver.php Line Number: 553
并且 2.
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
INSERT INTO `subcategories` (`study_education_level`, `tourism_country`, `tourism_place_name`, `post_id`) VALUES ('Higher Study', '', '', )
Filename: C:\xampp\htdocs\learn\system\database\DB_driver.php
Line Number: 331
提前谢谢你!
【问题讨论】:
-
query() 调用永远不会返回您在查询中选择的实际值。它们返回一个结果集,您必须从中获取一行(通常作为数组),然后从该数组中获取实际值。所以是的,你正试图将你的结果集对象填充到一个数组中并插入它,这意味着结果对象将尝试被 php 字符串化。
-
你需要对 Codeigniter 做更多的研究。阅读:codeigniter.com/userguide3/database/results.html
-
我同意你的看法。
标签: php mysql codeigniter