0

I am using MySQLi to create and use statements. For this I am using a divide and conquer strategy hopefully to make it so that I can create what I need to.

For this, I currently have 2 classes, a config file and my index file (this index is pointless to see).

The behaviour that I am currently expecting is for an sql statement to be prepared ready for working with printed to the screen using var_dump, but, what I get is an fatal error:

Call to a member function bind_param() on boolean.

This occurs in my Logger class, this class is as follows:

<?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);
    }
}
?>

As you can see, there is a global $link statement in this, and this is got from my config file below:

<?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");
?>

The database connection that is created, is done with this class:

<?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);
    }
}
?>

I know that the connection is live as I have tested this out of the Logger class using var_dump($link); and also by querying the database.

Edit:

Error from $link->error:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':heading, :body, :refNumber)'

Can O' Spam
  • 2,718
  • 4
  • 19
  • 45
  • Check the output of the query? if I'm correct it would only return a boolean if it returns a false? – Tredged Dec 23 '15 at 09:35
  • @Tredged, which query do you mean? The `$link->prepare(...)` or the ones that I tested with? – Can O' Spam Dec 23 '15 at 09:36
  • 1
    The prepare is failing and returning false. Use [mysqli_error](http://php.net/mysqli_error) and find out what's wrong. – Jonnix Dec 23 '15 at 09:37
  • @SamSwift웃 You should use error reporting, because the query is failing. thats the reason for the boolean thingy. – Tredged Dec 23 '15 at 09:38
  • @JonStirling, I have added the output from that at the bottom – Can O' Spam Dec 23 '15 at 09:39
  • @Tredged, what can I do to fix this? I have used this before and it has worked... – Can O' Spam Dec 23 '15 at 09:39
  • Question for dupe tag does not answer this for me. Please re-open as this requires an answer – Can O' Spam Dec 23 '15 at 09:42
  • 2
    mysqli only supports `?` as a placeholder. You should peruse the manual again, this is not how you use the mysqli API. – deceze Dec 23 '15 at 09:42
  • 1
    What @deceze said. Read the documentation for mysqli, it looks like you've been reading the PDO docs when you're not using it. – Jonnix Dec 23 '15 at 09:43
  • Also, don't `real_escape_string` when you're binding parameters. One or the other (preferably the latter), never both. – deceze Dec 23 '15 at 09:46

0 Answers0