codeigniter 致命错误:对Code igniter中的布尔值调用成员函数num_rows()

kb5ga3dv  于 2022-12-07  发布在  其他
关注(0)|答案(6)|浏览(129)

我曾面临这个致命的错误:本地问题。

$rr = $this->CI->db->query("select * from $table $condition ");

$mm = $rr->num_rows();

if ($mm>0) {

    return 1;

} else {

    return 0;

}
zi8p0yeb

zi8p0yeb1#

<?php 

$this->db->select("your stuff");
$query = $this->db->get("your table");

if($query->num_rows() > 0 ) {
     return 1;
}
else {
     return 0;
}

?>
a6b3iqyw

a6b3iqyw2#

$this->CI->db->query("select * from $table $condition ");

删除配置项和

$this->db->query("select * from $table $condition ");
oiopk7p5

oiopk7p53#

首先让您自动加载数据库

$autoload['libraries'] = array('database');

如果您尝试加载库,请使用$CI示例http://www.codeigniter.com/user_guide/general/ancillary_classes.html#get-instance

应用程序〉库〉示例.php

<?php

class Example {

   protected $CI;

   public function __construct() {
       $this->CI =& get_instance();
   }

   public function somename() {
       $query = $this->CI->db->query("select * from $table $condition ");

       if ($query->num_rows() > 0) {

       return 1; // return true;

       } else {

       return 0; // return false;

       }
   }

}

如果在模型上

应用程序〉模型〉示例模型.php

<?php

class Example_model extends CI_Model {

   public function __construct() {
     parent::__construct();
   }

   public function somename() {
       $query = $this->db->query("select * from $table $condition ");

       if ($query->num_rows() > 0) {

       return 1; // return true;

       } else {

       return 0; // return false;

       }
   }

}
ajsxfq5m

ajsxfq5m4#

更新一个CI核心文件并解决错误:
转到:系统/数据库/并编辑DB_active_rec.php文件转到第990行并查看吹塑代码

if ($query->num_rows() == 0)
{
    return 0;
}

更新至

if (!$query || $query->num_rows() == 0)
{
    return 0;
}

希望已解决您的num_rows()问题。

cotxawn7

cotxawn75#

类Site_m扩展MY_Model {

protected $_table_name = 'setting';
protected $_primary_key = 'option';
protected $_primary_filter = 'intval';
protected $_order_by = "option asc";

function __construct() {
    parent::__construct();
}

function get_site($id) {
    $compress = array();
    $query = $this->db->get('setting');
    foreach ($query->result() as $row) {
        $compress[$row->fieldoption] = $row->value;//here I am getting error
    }
    return (object) $compress;
}

}
/* 文件site_m.php结束 */

dvtswwa3

dvtswwa36#

此问题已在核心CI-2文件\系统\数据库\DB_驱动程序. php中修复
代码行https://github.com/bcit-ci/CodeIgniter/blob/develop/system/database/DB_driver.php#L659
在结束代码前替换FALSE

// Run the Query
    if (FALSE === ($this->result_id = $this->simple_query($sql)))
    {
        if ($this->save_queries == TRUE)
        {
            $this->query_times[] = 0;
        }

        // This will trigger a rollback if transactions are being used
        $this->_trans_status = FALSE;

        if ($this->db_debug)
        {
            // grab the error number and message now, as we might run some
            // additional queries before displaying the error
            $error_no = $this->_error_number();
            $error_msg = $this->_error_message();

            // We call this function in order to roll-back queries
            // if transactions are enabled.  If we don't call this here
            // the error message will trigger an exit, causing the
            // transactions to remain in limbo.
            $this->trans_complete();

            // Log and display errors
            log_message('error', 'Query error: '.$error_msg);
            return $this->display_error(
                                    array(
                                            'Error Number: '.$error_no,
                                            $error_msg,
                                            $sql
                                        )
                                    );
        }

        return FALSE;
    }

结束日期

// Run the Query
    if (FALSE === ($this->result_id = $this->simple_query($sql)))
    {
        if ($this->save_queries == TRUE)
        {
            $this->query_times[] = 0;
        }

        // This will trigger a rollback if transactions are being used
        $this->_trans_status = FALSE;

        if ($this->db_debug)
        {
            // grab the error number and message now, as we might run some
            // additional queries before displaying the error
            $error_no = $this->_error_number();
            $error_msg = $this->_error_message();

            // We call this function in order to roll-back queries
            // if transactions are enabled.  If we don't call this here
            // the error message will trigger an exit, causing the
            // transactions to remain in limbo.
            $this->trans_complete();

            // Log and display errors
            log_message('error', 'Query error: '.$error_msg);
            return $this->display_error(
                                    array(
                                            'Error Number: '.$error_no,
                                            $error_msg,
                                            $sql
                                        )
                                    );
        }

        $CR = new CI_DB_result();
        return  $CR;
    }

默认值应为返回空对象

$CR = new CI_DB_result();
return  $CR;

相关问题