调用非对象PHP帮助上的成员函数prepare() [英] Call to a member function prepare() on a non-object PHP Help
问题描述
我正在尝试编写PHP函数.这很简单.这只是一个查询数据库的准备好的语句,但是我无法使它正常工作.我一直收到非对象上的错误消息,调用成员函数prepare().这是代码:
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();
每当我调用该函数时,都会收到该错误.有人可以帮忙吗?
Any time I call the function i get that error. Can someone please help?
推荐答案
这是范围界定错误.您正在将$DBH
设置为全局变量.因此,当您输入函数时,全局变量不可用.您有5个实际选择.
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.使用全局关键字
function doSomething() {
global $DBH;
//...
这不是一个好主意,因为它使维护和测试PITA成为可能.想象一下尝试调试该函数调用.现在,您需要找出定义了$DBH
的位置,以尝试弄清发生了什么...
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
用作函数的参数
2. Make $DBH
a parameter to the function
function doSomething(MySQLi $DBH) {
它具有显式的优点.但这仍然不是很好,因为调用代码随后需要跟踪全局变量.
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.创建一个函数以获取" $DBH
对象
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();
}
这具有完全解决全局变量问题的优点.但是,也很难建立多个连接或将任何代码重用于其他连接.
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.创建一个包装数据库访问权限的类
class Database {
public function __construct($host, $user, $pass) {
$this->DBH = new MySQli($host, $user, $pass);
}
public function doSOmething() {
$this->DBH->foo();
}
}
这将为您封装所有内容.所有数据库访问都将通过一个类进行,因此您不必担心全局变量访问或其他任何问题.
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.使用预先构建的类/框架
这是最好的选择,因为您不必担心自己做.
This is the best option, since you don't need to worry about doing it yourself.
数据库访问类:
- 快速的Google搜索让您入门
- Doctrine ORM -具有完整ORM(对象映射)的完整数据库访问库
- ADODB -与数据库无关的数据库访问库
- Pear MDB2 -另一个数据库访问库
- 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 Framework
- 锂框架
- 代码点火器
- (真的还有很多,我不会再麻烦列出了,因为这是另一个问题……)
- Zend Framework
- Lithium Framework
- Code Igniter
- (really there are a lot more, I'm not going to bother listing any more since that's another question all together...)
真的,选择是无止境的.找到喜欢的东西,坚持下去.确实会让您的生活更轻松...
Really, the choices are endless. Find something you like, and stick with it. It really will make your life easier...
祝你好运!
这篇关于调用非对象PHP帮助上的成员函数prepare()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!