1

I've been trying to get prepared statements working - however, I keep running into the following error

<b>Fatal error</b>:  Call to a member function bindParam() on a non-object on line <b>41</b><br />

I have copied exactly many tutorials and even the provided code did not work and threw the same error.

My code is below:

$mysqli = new mysqli(connect, username,pass, datatbase);
$name = 'Tester';
if (mysqli_connect_errno()) {
    echo "Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error();
   }
      $stmt = $mysqli->prepare("INSERT INTO Parks VALUES (null,?,?,?,?,?,?,?,?,?,?,?,?,?,Now(),?,?,?, 0, 0, 0)");
  if ($stmt === FALSE) {
      die ("Mysql Error: " . $mysqli->error);
}
    $stmt->bind_param('ssssssssssssssss', $name, $theme, $size, $mountains, $hills, $river, $lake, $island, $setofislands, $ocean, $waterfalls, $file, $image, $description, $author,$cs);

$stmt->execute();
$stmt->close();
$mysqli->close();

It's the BindParam Line causing the error.

thanks in advance :)

EDIT: Error resolved, however, no data is being inserted into the database. EDIT: Updated query, database contains VARCHARs except for Description which is LONGTEXT. The final 3 are ints/doubles and there is a current date field.

user1662290
  • 466
  • 3
  • 10
  • 18
  • Duplicate of [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/15447204#15447204) – Your Common Sense Apr 22 '13 at 11:40
  • 1
    Try `$stmt->bind_param('s', $name);` and change your insertion point to '?' rather than just ? perhaps – Dave Apr 22 '13 at 12:01
  • @Dave is correct about bind_param needing to use 's', however I don't think the ? needs to be in quotes as that is the whole point of prepare/bind param. The current SQL is missing a close bracket for the VALUES after Now(). – MatthewMcGovern Apr 22 '13 at 12:05
  • @MatthewMcGovern it was a stab in the dark tbh with the ' but I think his main problem is as you say the missing close ) I didn't even notice that. The old wood for the trees problem I guess – Dave Apr 22 '13 at 12:09
  • ah yes, thanks for that! I've corrected it an there is no error. However, when I check my Database, the data hasn't been inserted.... – user1662290 Apr 22 '13 at 12:41

1 Answers1

2

bindParam is the PDO function. You are using mysqli so try bind_param instead. Where you have 'name' should also be the type definition, so you need 's' for string.

E.g:

$stmt->bind_param('s', $name);

Edit: Although saying that, the error doesn't say the function is incorrect. It says the object doesn't exist... Running this could would give you information as to why the prepare is failing.

$stmt = $mysqli->prepare("INSERT INTO 'Parks' VALUES(null, ?");

if ($stmt === FALSE) {
    die ("Mysql Error: " . $mysqli->error);
}

Most likely the prepare is failing as the SQL is incorrect (My guess is the table name 'Parks' should NOT be in qutoes)

Edit 2: My guess for it still not working is:

$stmt->bindParam('name', $name);

Where you have 'name' should actually be the variable type, as in integer, double, string, etc. This is so the database knows what your variable is.

Try replacing that line with:

$stmt->bindParam('s', $name);

MatthewMcGovern
  • 3,466
  • 1
  • 19
  • 19
  • Hi, thanks for the response I originally tried without the quotes but that didn't work either. I've implemented your code and I've updated my question – user1662290 Apr 22 '13 at 11:53
  • @user1662290: You are missing a closing bracket `)` after `Now()` in the prepare string. – MatthewMcGovern Apr 22 '13 at 12:00
  • Thanks. the code works now with no error - however, I've checked the database and no data is being inserted. – user1662290 Apr 22 '13 at 14:21
  • @user1662290 Can you update your original question with the code you are currently using? Then I will take a look. – MatthewMcGovern Apr 22 '13 at 14:23
  • All that was missing was the extra bracket ... but I've updated the question. – user1662290 Apr 22 '13 at 14:36
  • @user1662290 Okay, just wanted to make sure. I have edited my answer again. – MatthewMcGovern Apr 22 '13 at 14:49
  • Hi, I created on a new table that only had one column and it worked fine. I've updated the post again to show the entire query. I was hoping that once the snippet I posted earlier worked then the rest would fall into place. – user1662290 Apr 22 '13 at 15:39
  • @user1662290 You need one variable type for each variable, I am assuming all the values you are passing in are strings `$stmt->bind_param('ssssssssssssssss', $name, $theme, $size, $mountains, $hills, $river, $lake, $island, $setofislands, $ocean, $waterfalls, $file, $image, $description, $author,$cs);` If they are not strings, use i for Integers, d for Doubles and b for Blob (binary dump) – MatthewMcGovern Apr 22 '13 at 15:45
  • Hi, Unfortunatly, still no luck. I am going to open a different question, since my orginal question has been solved. Thanks for helping me sort it out :D – user1662290 Apr 22 '13 at 16:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28677/discussion-between-matthewmcgovern-and-user1662290) – MatthewMcGovern Apr 22 '13 at 16:10