4

I am converting my old mysql_* queries over to mysqli except i'm having a frustrating issue

$st = $this->Sql->prepare("INSERT INTO tblPlayerOnline (SteamID,PlayerName,IPAddress,ConnectedDate) VALUES (?, ?, ?, ?)") or die($this->Sql->error);
/* 225 */ $st->bind_param("i", $SteamID);
$st->bind_param("ssi", $PlayerName, $IPAddress, $TimeNow);
$st->execute();

Fatal error: Call to a member function bind_param() on a non-object in /home/vanrust/public_html/rework/class/Players.class.php on line 225

But when I dump $this->Sql I get object(mysqli)#2 (19) { bla bla so not sure what this error is telling me.

Dump of $this->Sql http://pastebin.com/gdKAgT4D

Any guidance is appreciated!

PS Looked Everywhere -.-

mega6382
  • 9,211
  • 17
  • 48
  • 69
zanderwar
  • 3,440
  • 3
  • 28
  • 46

3 Answers3

1

When the prepare statement fails and no mysqli_prepare object is returned, there's a big chance you have an error inside your query.. or something worse.

For debugging purposes you can use the mysqli_report(MYSQLI_REPORT_ALL) function (which is an alias for mysqli_driver->report_mode). This can give you some more information why the prepare is failing and be helpfull for debugging.

Place the following in the beginning of your code

mysqli_report(MYSQLI_REPORT_ALL);

//Or if you just want an exception when Mysqli has an error? 
//Set the reporting level to 
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

Next to that I think it's very risky to always assume that the mysql_prepare object is returned. You should first check before using it:

if ($stmt = $this->Sql->prepare(....)) {
    //Yep, we can now start binding parameters
} else {
    //Noooooo.. got error?
}
Benz
  • 2,317
  • 12
  • 19
0

Ok so this frustrated me for awhile but I found the answer was in front of me all along right there in $this->Sql->stat - Oddly, 400 open tables seemed like a rather nice even number however this was clearly a maximum.

I tracked back through my code and began adding $st->close() to everything and this fixed this problem for me.

So lesson learned; close it once you're done with it

Thanks to all for helping

zanderwar
  • 3,440
  • 3
  • 28
  • 46
-3

Looks like $st is null, meaning that the statement failed to prepare.

Try to see what's causing it with this:

if (!$st) {
     echo "Prepare failed: (" . $this->Sql->errno . ") " . $this->Sql->error;
}
Frank
  • 664
  • 5
  • 15