简体   繁体   English

CodeIgniter数据库无所事事

[英]CodeIgniter Nothing happens database

Hi I am following a turtorial to learn CodeIgniter on NetTuts . 你好我下面一个turtorial学习笨的NETTUTS

I have one model, one view and one controller involved. 我有一个模型,一个视图和一个控制器。

The Model: 该模型:

class Data_model extends CI_Model{
  function getAll(){
      $data = array();

      $q = $this->db->query("SELECT * FROM data");
      if($q->num_row() > 0){
           foreach($q->result() as $row){
                $data[] = $row;
           }
           return $data;
      }
 }

} }

The View: 风景:

<html>
 <head>
      <title></title>
 </head>

 <body>
      <p>View loaded</p>
      <?php 
           foreach ($rows as $r){
                echo '<h1>',$r->title,'</h1>';
           }
      ?>
 </body>

The Controller: 控制器:

class Site extends CI_Controller {

 function index(){
      $this->load->model('data_model');
      $data['rows'] = $this->data_model->getAll();

      $this->load->view('home');
 }
}

The problem is that I don't get an error but just an empty page. 问题是我没有收到错误,而只有一个空白页。 What am I doing wrong? 我究竟做错了什么? It feels like I am doing everything from the tutorial? 感觉我正在完成本教程中的所有内容?

In controller: 在控制器中:

$this->load->view('home', $data);

Suggestion for model: 型号建议:

function getAll(){
    $data = array();
    $q = $this->db->query("SELECT * FROM data");
    if($q->num_row() > 0){
       $data    = $q->result(); //result_array() will fetch as arrays and not objects
       return $data;
    }
}

Try this one as it is already given by Paul : 尝试一下Paul已经给出的方法:

echo '<h1>' . $r->title . '</h1>';

Go to your root/index.php and change environment to developing to see the error generated by php. 转到您的root / index.php并更改developing environment以查看php生成的错误。

In the model, 在模型中

$q->num_row()

needs to be changed to 需要更改为

$q->num_rows()

Also, you need to reference the specific column too 另外,您还需要引用特定的列

$data[] = $row->column_name;

In your controller, the commas need to be replaced with periods. 在您的控制器中,逗号需要替换为句点。 Commas aren't proper syntax 逗号不是正确的语法

foreach ($rows as $r) {
    echo '<h1>'.$r->title.'</h1>';
}

Finally, you need to pass the $data variable to the view by doing 最后,您需要执行以下操作将$ data变量传递给视图

$this->load->view('home', $data);

另外,我认为您必须在视图中进行以下更改:

echo '<h1>' . $r->title . '</h1>';

This does work: 这确实有效:

class Data_model extends CI_Model{

 function getAll(){
 $data = array();
 $q = $this->db->query("SELECT * FROM data");


   $data= $q->result(); //result_array() will fetch as arrays and not objects
   return $data;


 }
}

But this dosn't: 但这不是:

class Data_model extends CI_Model{
 function getAll(){
 $data = array();
 $q = $this->db->query("SELECT * FROM data");

 if($q->num_row() > 0){
   $data= $q->result(); //result_array() will fetch as arrays and not objects
   return $data;
}

 }
}

But why is this the case, and is this even correct to do (is this even an answer), what consequences could be meet by removing this IF statement? 但是,为什么会这样呢?这样做是否正确(这甚至是一个答案),删除此IF语句可能会带来什么后果?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM