Skip to content
Advertisement

MySQLi Query returning fatal errors for an unknown reason

So, I’v just finished coding the base for a very simple script I’m working on except it’s returning

Fatal error: Call to a member function bind_param() on a non-object in
             C:wampwwwgamepalquoterincludesfunction.php on line 26

now I’v checked over line 26 and come to the conclusion it’s due to the above prepare failing except I can’t for the life of me workout why it is failing, I’v gone through the steps listed on several pages around about displaying errors, checking the connection etc and It didn’t get me any closer to fixing the issue. If anyone can shed light on why the below code is failing it would be amazingly appreciated!

I’v stripped the below code down to the bare minimum until I can it functioning, if anyone can see why it the bind_param is failing it would be greatly appreciated if you could point it out and possibly with a reference explaining so I don’t do it again in the future, thank you in advance.

config.php;

<?php
$host = 'localhost';
$username= 'root';
$password = '';
$db = 'testdb';

$con = mysqli_connect($host, $username, $password, $db);
   if ( !$con ) {
      die('Unable to connect to database');
   }

   mysqli_select_db($con,"testdb") or die("Unable to connect to testdb");
?>

function.php;

include_once "config.php";
   $quote = $_POST['quote'];
   $quoteby = $_POST['quoteby'];

      $execute = $con->prepare("INSERT INTO `quoter` VALUES (?, ?)");
         $execute->bind_param('ss', $quote, $quoteby);
            $execute->execute();
         $execute->close();
?>

Advertisement

Answer

$execute = $con->prepare("INSERT INTO `quoter` VALUES ('?', '?');");

is not a valid query. You should not quote the parameter place holders, and there should be no semicolon at the end of the statement (not sure if the latter gives an actual error though)

$execute = $con->prepare("INSERT INTO `quoter` VALUES (?, ?)");

The error makes prepare return FALSE, which has no method called bind_param(), which gives the exact error message you’re getting.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement