【问题标题】:`CI` return wrong value`CI` 返回错误值
【发布时间】:2017-08-26 10:25:27
【问题描述】:

我不知道为什么我的 CI 从 phpMyAdmin 返回错误值 型号:

    <?php

class Post extends CI_Model
{
function __construct()
{
    parent::__construct();

}
function getallpost()
{
    return $this->db->get('post');


}

}
?>

和

控制器:

    <?php 

defined('BASEPATH') OR exit('No direct script access allowed');

class Postc extends CI_Controller {
    function index()
    {
    $this->load->model('post');
    $posts=$this->post->getallpost();
    echo '<pre>';
    print_r($posts);

    }
}
?>

enter image description here

【问题讨论】:

    标签: codeigniter phpmyadmin


    【解决方案1】:

    您的代码没有给您任何错误,它完全按照您的要求执行。也许您需要具体说明您想要获取的信息。你在你的模型中看到了

    function getallpost()
    {
     return $this->db->get('post');
    }
    

    上面的函数会返回一个对象。如果你想得到一个数组,你需要写

    return $this->db->get('post')->result_array();
    

    并确保您的 post 表中有一些数据要打印要了解有关查询数据库的更多信息,您应该阅读 Codeigniter 的 Query Builder Class

    【讨论】:

    【解决方案2】:

    更改您的型号代码

    <?php
        class Post extends CI_Model
        {
            function __construct()
            {
                parent::__construct();
            }
            function getallpost()
            {
                $query = $this->db->get('post');
                if($query->num_rows() > 0)
                {
                    return $query->result();
                }else{
                    return false;
                }
            }
        }
    

    【讨论】:

    【解决方案3】:

    如果您想将结果作为数组返回,请使用

    return $this->db->get('post')->result_array();
    

    如果您想将结果作为对象返回,请使用

    return $this->db->get('post')->result();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      • 2019-02-03
      • 2015-06-19
      • 2014-01-16
      • 2019-10-13
      • 2020-10-05
      • 2015-11-19
      相关资源
      最近更新 更多