这是我第一次尝试在 CodeIgniter 中编写代码。问题是它无法从数据库中检索数据。它抛出的错误是:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Site::$db
Filename: core/Model.php
Line Number: 51
我可以理解在core/model.php
.
这是另一个错误:
Fatal error: Call to a member function query() on a non-object in C:\wamp\www\CI_one\application\models\site_model.php on line 9
控制器
<?php
class Site extends CI_Controller {
function datta(){
$this->load->model('site_model');
$get = $this->site_model->getValues();
$data['get'] = $get;
$this->load->view('data3',$data);
}
}
模型
<?php
class site_model extends CI_Model{
function index(){
echo "default index";
}
function getValues(){
$query = $this->db->query("select * from nav_menu");
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
echo $row->c1;
echo $row->m2;
echo $row->d3;
}
}
return array(
"name" => "nameless",
"form" => "formless",
"age" => "ageless");
}
}
我已经在app/config/database.php
.
我该如何进行这项工作?
我自己解决了。我擦掉并重新编码如下以获得结果......
控制器
<?php
class Site extends CI_Controller {
function datta(){
$this->load->database();
$this->load->model('site_model');
$get = $this->site_model->getValues();
$data['get'] = $get;
$this->load->view('data3',$data);
}
}
模型为
<?php
class site_model extends CI_Model{
function index(){
echo "default index";
}
function getValues(){
$query = $this->db->query("select * from nav_menu");
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
echo $row->c1;
echo $row->m2;
echo $row->d3;
}
}
return array(
"name" => "nameless",
"form" => "formless",
"age" => "ageless");
}
}