我目前是CodeIgniter的初學者,我試圖獲得一個簡單的MVC數據庫的東西,但它不會。我試圖從表格中選擇一條記錄並將其顯示到網頁上,但我得到的卻是錯誤。我會後我下面的代碼,所以你可以看到我一起工作:codeigniter數據庫代碼不起作用
型號:
function grabData() {
$sql = "SELECT * FROM books WHERE id = 1";
$config['hostname'] = "localhost";
$config['username'] = "root";
$config['password'] = "";
$config['database'] = "bookstore";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
// manually connect to database
$this->load->database($config, TRUE);
// do some stuff
$query = $this->db->get('books');
if ($query->num_rows() > 0) {
return true;
} else {
return false;
}
}
控制器:
$web['title'] = "CI Hello World App!";
$this->load->view('helloworld_view', $web);
$this->load->model('helloworld_model');
$data['result'] = $this->helloworld_model->grabData();
$this->load->view('helloworld_view', $data);
表的內容:
1 The Grapes of Wrath John Steinbeck 12.99
2 Ninteen Eighty-Four George Orwell 8.99
3 The Wind-Up Bird Chronicle Haruki Murakami 7.99
錯誤:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: result
Filename: views/helloworld_view.php
Line Number: 8
null
boolean true
,因爲我覺得這不是問題的根源沒顯示視圖。任何幫助,將不勝感激。謝謝!
你得到的查詢結果是否正確? – Shaolin 2013-03-04 06:21:37
你知道你正在返回任何表格內容,對吧?你爲什麼要加載你的觀點兩次? – 2013-03-04 06:22:21
我的看法只是一個空白的網頁,其結果變量如此回顯:<?php echo $ result; ?> – JaPerk14 2013-03-04 06:26:05