0

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:\wamp\www\gamepal\quoter\includes\function.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();
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ben F
  • 27
  • 6
  • 2
    Yet another link, just to make your ones not to feel alone: [Mysqli update throwing Call to a member function bind_param() error](http://stackoverflow.com/questions/15447133/mysqli-update-throwing-call-to-a-member-function-bind-param-error) – Your Common Sense Jun 02 '13 at 11:50
  • Fixed the issue, Issue was I have 3 table fields but I only had 2 paramters in my SQL query, I added (quote, quoteby) VALUES(?,?) and now It is working, sorry for bothering everyone, This completely flew over my head as I believed AUTO_INC meant it would automatically exclude that table field. – Ben F Jun 02 '13 at 12:26
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Jan 01 '20 at 00:36

3 Answers3

4
$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.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • Even after fixing the above errors (Which I put in in an attempt to get it working) it still returns the exact same error with $execute = $con->prepare("INSERT INTO `quoter` VALUES (?, ?)"); I Really don't understand, I learnt this directly from the php.net website for mysqli, so it can't be wrong, can it? :S – Ben F Jun 02 '13 at 12:06
  • Ok, It appears the issue may be that the table has 3 fields, id quote and quoteby, but I'm only defining 2. Issue is id is AUTO_INC so I don't understand why I would have to insert into it :S Sorry guys, I'm only just learning mysqli – Ben F Jun 02 '13 at 12:22
1

remove '' quote on ?

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

  • Even after fixing the above errors (Which I put in in an attempt to get it working) it still returns the exact same error with $execute = $con->prepare("INSERT INTO `quoter` VALUES (?, ?)"); – Ben F Jun 02 '13 at 12:06
0

Following on from Ben F's comment.

Change your query to

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

This way you don't need to specify the id value as you insert rows.

nurdglaw
  • 2,107
  • 19
  • 37
  • I think I commented my initial post at the same time you posted this, Thank you for your post though, Few seconds earlier and you would have helped me quite a bit more. – Ben F Jun 02 '13 at 12:30