【问题标题】:CodeIgniter No rows returned from get()CodeIgniter 没有从 get() 返回的行
【发布时间】:2013-06-24 08:01:37
【问题描述】:

我已经搞砸了几个小时了。这一定是愚蠢的,但我只是没有看到它。为什么我的 db->get() 不返回任何行?这是我第一次尝试 MVC 框架。我已经在 Ubuntu 中安装了 apache2、mysql、php5 和 codeigniter。出于某种原因,我似乎无法让模型访问数据库中的数据。

# cat config/database.php 
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = '127.0.0.1';
$db['default']['username'] = 'webuser';
$db['default']['password'] = 'webuser';
$db['default']['database'] = 'product';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

# cat models/productmodel.php 
<?php
    class Productmodel extends CI_Model {

            public function __construct() {
                    parent::__construct();
                    $this->load->database();
            }

            public function ListData() {

                    $query = $this->db->get("Fruit");

                    if ($query->num_rows() > 0) {
                            foreach ( $query->result() as $row ) {
                                    echo "VALUE: <BR>\n";
                            }
                    } else {
                            $string = $this->db->last_query();
                            echo "$string <BR>\n";
                    }

                    return $query->result();
            }
    }
?>

# cat controllers/productcontroller.php 
<?php
    class Productcontroller extends CI_Controller {
            public function index() {
                    $this->load->model('productmodel');

                    $data['query'] = $this->productmodel->ListData();

                    $data['title'] = 'MVC Test';
                    $data['heading'] = 'Data From product.Fruit';
                    $this->load->view('content', $data);
            }
    }
?>

# cat views/content.php 
<html>
  <head>
    <title><?php echo $title;?></title>
  </head>
  <body>
    <h1><?php echo $heading;?></h1>
    <?php foreach ($query AS $value) {echo "Value: $value <BR>\n"; } ?>
  </body>
</html>

# echo "SELECT * FROM product.Fruit" | mysql -h 127.0.0.1 -u webuser --password=webuser
name      color   size
Apple      Red     1
Orange     Orange  1
Watermelon Green   3
Grape      Green   0
Cherry     Red     0
HoneyDew   Green   2

【问题讨论】:

  • 您的错误日志是否报告了什么?这是where to find the PHP error log的信息。
  • 你的意思是$query = $this-&gt;db-&gt;get("Fruit");不返回任何结果?这不可能。如果将db-&gt;get() 替换为db-&gt;query('SELECT * FROM Fruit); 会发生什么?

标签: php mysql codeigniter database-abstraction


【解决方案1】:

试试这个:

<?php
    $data   = array();                                  #array declaration
    $query  = $this->db->get("Fruit");                  #actual get all query
    if($query->num_rows() > 0){                         #checking for the existence of rows
        $data   = $query->result_array();               #will fetch all the tuples in the data array 
    }
    echo "<pre>";print_r($data);echo "</pre>";die;      #print the data array and die
?>

【讨论】:

    【解决方案2】:

    Here 所述 使用 $this-&gt;db-&gt;last_query(); 调试

    【讨论】:

      【解决方案3】:

      试试这个。它会给你表格中的数据和表格包含的记录数。

      public function your_method()
      {      
              $this->db->select('*')->from('your_table');
              $query = $this->db->get();
              if($query->num_rows() > 0) 
              {
                  $res['your_data']    = $query->result(); 
                  $res['num_rows']     = $query->num_rows();
      
              }
              //if your_table contains data/records or not the following code will give you result in both the cases. 
              echo "<pre>";print_r($res);die; 
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-13
        • 2012-01-22
        • 2015-04-16
        • 2012-07-23
        • 2021-08-13
        相关资源
        最近更新 更多