【问题标题】:Codeigniter, Database Errror, Severity: 4096 Message: Object of class CI_DB_mysql_result could not be converted to stringCodeigniter,数据库错误,严重性:4096 消息:CI_DB_mysql_result 类的对象无法转换为字符串
【发布时间】: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);

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

  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.

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


【解决方案1】:

顺便说一句,我已经解决了这个问题:)

$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;
                    }
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 2013-12-13
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多