問題描述
我正在嘗試編寫一個(gè) PHP 函數(shù).這很簡單.這只是一個(gè)查詢數(shù)據(jù)庫的準(zhǔn)備好的語句,但我無法讓它工作.我不斷收到錯(cuò)誤 Call to a member function prepare() 在非對(duì)象上.這是代碼:
I am trying to write a PHP function. It is very simple. It is just a prepared statement that queries the database, but I can not get this to work. I keep recieving the error Call to a member function prepare() on a non-object. here is the code:
$DBH = new mysqli("host", "test", "123456", "dbname");
function selectInfo($limit, $offset){
$stmt = $DBH->prepare("SELECT * FROM information LIMIT ?,?");
$stmt->bind_param("ii", $limit, $offset);
$stmt->execute();
}
selectInfo();
每當(dāng)我調(diào)用該函數(shù)時(shí),我都會(huì)收到該錯(cuò)誤.有人可以幫忙嗎?
Any time I call the function i get that error. Can someone please help?
推薦答案
這是一個(gè)范圍錯(cuò)誤.您正在使 $DBH
成為一個(gè)全局變量.所以當(dāng)你進(jìn)入函數(shù)時(shí),全局變量是不可用的.您有 5 種實(shí)??際選擇.
It's a scoping error. You're making $DBH
a global variable. So when you enter the function, the global variable is not available. You have 5 real options.
1.使用全局關(guān)鍵字
function doSomething() {
global $DBH;
//...
這不是一個(gè)好主意,因?yàn)樗咕S護(hù)和測試成為 PITA.想象一下嘗試調(diào)試該函數(shù)調(diào)用.您現(xiàn)在需要找出 $DBH
的定義位置,以嘗試弄清楚發(fā)生了什么......
This is not a good idea, since it makes maintenance and testing a PITA. Imagine trying to debug that function call. You now need to go find out where $DBH
is defined to try to figure out what's going on...
2.將 $DBH
作為函數(shù)的參數(shù)
2. Make $DBH
a parameter to the function
function doSomething(MySQLi $DBH) {
它的優(yōu)點(diǎn)是明確.但這仍然不是很好,因?yàn)檎{(diào)用代碼需要跟蹤全局變量.
It has the advantage of being explicit. But it's still not great since the calling code then needs to keep track of the global variable.
3.創(chuàng)建一個(gè)函數(shù)來獲取"$DBH
對(duì)象
3. Create a function to "get" the $DBH
object
function getDBH() {
static $DBH = null;
if (is_null($DBH)) {
$DBH = new mysqli(...);
}
return $DBH;
}
function doSomething() {
$DBH = getDBH();
}
這樣做的好處是完全解決了全局變量問題.但是也很難有多個(gè)連接或?qū)⑷魏未a重用于其他連接.
This has the advantage of getting around the global variable problem completely. But it's also hard to have multiple connections or re-use any of the code for other connections.
4.創(chuàng)建一個(gè)類來包裝數(shù)據(jù)庫訪問
class Database {
public function __construct($host, $user, $pass) {
$this->DBH = new MySQli($host, $user, $pass);
}
public function doSOmething() {
$this->DBH->foo();
}
}
這為您封裝了所有內(nèi)容.所有數(shù)據(jù)庫訪問都將通過一個(gè)類進(jìn)行,因此您無需擔(dān)心全局變量訪問或其他任何事情.
This encapsulates everything for you. All database access will go through a single class, so you don't need to worry about global variable access or anything else.
5.使用預(yù)先構(gòu)建的類/框架
這是最好的選擇,因?yàn)槟鸁o需擔(dān)心自己動(dòng)手.
This is the best option, since you don't need to worry about doing it yourself.
數(shù)據(jù)庫訪問類:
- 快速谷歌搜索以幫助您入門
- Doctrine ORM - 具有完整 ORM(對(duì)象映射)的完整數(shù)據(jù)庫訪問庫
- ADODB - 一個(gè)與數(shù)據(jù)庫無關(guān)的數(shù)據(jù)庫訪問庫
- Pear MDB2 - 另一個(gè)數(shù)據(jù)庫訪問庫
- A quick google search to get you started
- Doctrine ORM - A complete database access library with full ORM (Object Mapping)
- ADODB - A database agnostic database access library
- Pear MDB2 - Another database access library
完整框架:
- Zend 框架
- 鋰框架
- 代碼點(diǎn)火器
- (真的還有很多,我不打算再列出來了,因?yàn)槟鞘橇硪粋€(gè)問題……)
真的,選擇是無窮無盡的.找到你喜歡的東西,并堅(jiān)持下去.它真的會(huì)讓你的生活更輕松......
Really, the choices are endless. Find something you like, and stick with it. It really will make your life easier...
這篇關(guān)于調(diào)用非對(duì)象 PHP 幫助上的成員函數(shù) prepare()的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!