使用$ mysqli->准备不工作

时间:2015-12-23 09:34:17

标签: php mysqli parameters

我正在使用MySQLi来创建和使用语句。为此,我使用划分并征服策略希望来制作它,以便我可以创建我需要的东西。

为此,我目前有2个类,一个配置文件和我的索引文件(这个索引没有意义)。

我目前期待的行为是准备好使用var_dump打印到屏幕上的sql语句,但是,我得到的是致命错误:

  

在布尔值上调用成员函数bind_param()。

这发生在我的Logger类中,这个类如下:

<?php
class Logger
{
    private $lastLoggged;
    private $lastLogId;

    public function __construct() {  }

    public function Log($heading, $body, $refNum)
    {
        global $link;
        if (!$link)
            die("Cannot connect to database.");
        $heading = $link->real_escape_string($heading);
        $body = $link->real_escape_string($body);
        $refNum = $link->real_escape_string($refNum);
        $log_qry = $link->prepare("INSERT INTO `logs` (`heading`, `body`, `ref_num`) VALUES (:heading, :body, :refNumber);");
        $log_qry->bind_param(":heading", $heading);
        $log_qry->bind_param(":body", $body);
        $log_qry->bind_param(":refNumber", $refNum);

        var_dump($log_qry);
    }
}
?>

正如您所看到的,此处有一个global $link语句,这可以从我的配置文件中获取:

<?php
require_once 'classes/Database.php';
$db = new Database();
$link = $db->mysqli;

require_once 'classes/Logger.php';
$log = new Logger();
$log->Log("heading", "body", "#123");
?>

创建的数据库连接是使用此类完成的:

<?php
class Database
{
    private $hostname;
    private $username;
    private $password;
    private $database;
    /** @var mysqli */
    public $mysqli;

    public function __construct()
    {
        $this->hostname = "localhost";
        $this->username = "root";
        $this->password = "";
        $this->database = "sites";

        $this->mysqli = new mysqli($this->hostname, $this->username, $this->password, $this->database);
    }
}
?>

我知道连接是实时的,因为我使用Logger并通过查询数据库来测试var_dump($link);类。

修改

来自$link->error的错误:

  

您的SQL语法有错误;检查与MariaDB服务器版本对应的手册,以便在':heading,:body,:refNumber'附近使用正确的语法''

0 个答案:

没有答案