Codeigniter,Database Errror,严重性:4096消息:类CI_DB_mysql_result的对象无法转换为字符串

时间:2015-07-24 20:02:33

标签: php mysql codeigniter

我试图从名为" 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);

但每当我运行代码时,它都会给我两个ERROR。

  1. 遇到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. 和 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
    

    先谢谢你!

1 个答案:

答案 0 :(得分:0)

顺便说一下我解决了问题:)

$result = $this->db->query($copy_query);    //adding that post_id to the variable   
            if ($result->num_rows() > 0)
            {
                    foreach ($result->result() as $row)
                    {
                            $post_id_plz =  $row->post_id;
                    }
            }