调用非对象 PHP 帮助上的成员函数 prepare() [英] Call to a member function prepare() on a non-object PHP Help

查看:18
本文介绍了调用非对象 PHP 帮助上的成员函数 prepare()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 PHP 函数.这很简单.这只是一个查询数据库的准备好的语句,但我无法让它工作.我不断收到错误 Call to a member function 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.

数据库访问类:

  • 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

完整框架:

真的,选择是无穷无尽的.找到你喜欢的东西,并坚持下去.它真的会让你的生活更轻松......

Really, the choices are endless. Find something you like, and stick with it. It really will make your life easier...

这篇关于调用非对象 PHP 帮助上的成员函数 prepare()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆