我的CodeIgniter项目中遇到了一个php错误

时间:2016-07-28 21:36:01

标签: php mysql codeigniter mysqli

我想在CodeIgniter中设置我的第一个项目,其中包含一个数据库。 我已为数据库进行了正确的配置,如本教程中所示:Everything You Need to Get Started With CodeIgniter

我在phpMyadmin中创建了我的数据库。 当我尝试在我的localhost中访问项目时,这是错误: enter image description here 从&root;'更改密码后到' '现在错误是这样的:

<?php
class diploma_model extends Model {
 
    function diploma_model()
    {
        // Call the Model constructor
        parent::Model();
    }
     
    function getData()

        {
            //Query the data table for every record and row
            $query = $this->db->get('data');
             
            if ($query->num_rows() > 0)
            {
                //show_error('Database is empty!');
            }else{
                return $query->result();
            }
        }
 
}
?>

这是我的模特cpde:

&#13;
&#13;
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
&#13;
&#13;
&#13;

我是codeIgniter的新手。有人能解释为什么会出现这种错误吗谢谢!

2 个答案:

答案 0 :(得分:2)

您阅读的教程已经过时了。当前的codeigniter网站是here

值得一读here

这是CI模型和anatomy-of-a-model的正确方法,我认为因为您只是扩展模型而没有CI_Model

你的构造错了。

在autoload.php上我会自动加载数据库。

<?php    

class Diploma_model extends CI_Model {

public function __construct() {

    // Call the Model constructor
    parent::__construct();
}

function getData() {
  $query = $this->db->get('data');

  // This > means greater. So will return results if any found!

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

     return $query->result();

     //return $query->result_array();

   } else {

       return false;

    }
  }

}

文件名Diploma_model.php

<?php 

class Example extends CI_Controller {

   public function __construct() {
      parent::__construct();
      $this->load->model('diploma_model');
   }

   public function index() {
      $data['lists'] = $this->diploma_model->getData();

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

}

加载model

is.sequence <- function(x)
    all(apply(head(cbind(x-1, x, x+1), -1) - x[-1] == 0, 1, any))

tapply(dt$seq, dt$group, is.sequence)
#    a     b     c 
# TRUE FALSE  TRUE 

models

的CI用户指南

答案 1 :(得分:1)

转到application / config / database.php文件,并设置用户,传递以及您想要的任何数据库配置。

你会看到这样的事情:

(...)
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'database_name',
'dbdriver' => 'mysqli',
(...)