0

I got some problem with binding some parameters in MYSQL statement in php. It is throwing an error when count($posts) > 1 on the marked line below. Anyone who know what I've done wrong? The error is: Call to a member function bind_param() on a non-object. It is also reporting comman out of sync?(on the marked line below)

<?php 

include '../../main/mainFunctions2.php';

$futurePosts = json_decode($_POST['futurePosts']);

$repeatSerie = null;
if(count($posts) > 1){

    //Get new repeatSeries
    $stmt = $mysqli->prepare("
    SELECT repeatSerie
    FROM timeSpaces_futurePosts
    ORDER BY repeatSerie DESC
    LIMIT 1
    ");
    $stmt->execute();
    $stmt->bind_result($repeatSerie);
    $stmt->fetch();
    $repeatSerie = ((int)$repeatSerie + 1);
}
$timeStamp = time();
foreach($posts as $fp){
    $title = $fp->title;
    $startDate = $fp->startDate;
    $endDate = $fp->endDate;
    $startTime = $fp->startTime;
    $endTime = $fp->endTime;
    $location = $fp->location;
    $latLong = $fp->latLong;
    $info = $fp->info;
    $photoId = $fp->photoId;
    $invited = $fp->invited;
    if($invited != null){
        $invited = 1;
    }else{
        $invited = 0;
    }
    $reminderType = $fp->reminderType;
    $reminderTimeStamp = $fp->reminderTimeStamp;
    $repeatSerie = $repeatSerie;

    $stmt = $mysqli->prepare("
    INSERT INTO futurePosts (profileId, title, startDate, endDate, startTime, endTime, location, latLong, info, photoId, invited, reminderType, reminderTimeStamp, repeatSerie)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
    );
    $stmt->bind_param('isssiisssiisii', $profileId, $title, $startDate, $endDate, $startTime, $endTime, $location, $latLong, $info, $photoId, $invited, $reminderType, $reminderTimeStamp, $repeatSerie);
    //The line above: Call to a member function bind_param() on a non-object
    $stmt->execute();
    $futurePostId = $mysqli->insert_id;

    if($invited == 1){
        foreach($fp->invited as $friendsId){
            $friendsId = $friendsId;
            $stmt = $mysqli->prepare('
            INSERT INTO futurePosts_invited (profileId, futurePostId, timeStamp)
            VALUES (?, ?, ?)
            ');
            $stmt->bind_param('iii', $friendsId, $futurePostId, $timeStamp);
            $stmt->execute();
        }
    }
}

echo 'TRUE';


?>
TML
  • 12,813
  • 3
  • 38
  • 45
einstein
  • 13,389
  • 27
  • 80
  • 110
  • Probably related (I can't see anything wrong with the SQL, but there's bound to be something wrong with it): [How to squeeze error message out of PDO?](http://stackoverflow.com/q/3726505) – Pekka Aug 10 '11 at 21:40
  • command out of sync: can't use the prepare statement now. What's that kind of error? I got that with mysqli_report(MYSQLI_REPORT_ALL); on same line – einstein Aug 10 '11 at 21:43
  • I also get three question mark in the prepare statement on the error log – einstein Aug 10 '11 at 21:44
  • check out http://php.net/manual/en/mysqli.query.php which has some info on the error you quote - apparently, you need to free result sets before making a new query – Pekka Aug 10 '11 at 21:45
  • Ok I solved it! I need to close statement when I fetch the result and move to the next result set. `$stmt->close(); while($mysqli->next_result()) { } ` – einstein Aug 10 '11 at 21:52
  • Woho, you can also call `$stmt->store_result();` after `SELECT` statement is executed. This should also fix it. – Mchl Aug 10 '11 at 21:59

2 Answers2

3

This is most likely because $stmt = $mysqli->prepare(...); line fails due to SQL syntax error. Try echoing $mysqli->error to see what's wrong with it.

Try calling $stmt->store_result(); after execution of your SELECT statement and before issuing any other queries to MySQL.

Side note: you should prepare your statement before foreach loop. That will get you a bit of performance gain, since the statement will only be compiled once and only parameters will be sent to server on each loop run.

Mchl
  • 61,444
  • 9
  • 118
  • 120
  • I think this would actually SOLVE the problem - you're trying to prepare a second time, inside the foreach() loop, without having called 'close' on the mysqli statement, which results in the "command out of synch" error. – TML Aug 10 '11 at 21:52
  • I'm not sure about that @TML (that is, I'm pretty sure it is not the case). Assigning a second statement to `$stmt` variable should dereference previous statement object and close it automatically as part of object destruction. – Mchl Aug 10 '11 at 21:54
  • You could be right - I'm actually not much of a mysql user - but I'm pretty sure here that the parent $mysqli object still knows that it has an open prepared statement and you have to close it / free the results to stop getting the "commands out of sync" error. – TML Aug 10 '11 at 21:56
  • 1
    Ah wait. There's also a `SELECT` statement before the loop. That would be it. A `$stmt->store_result();` should be called before any new queries are send to MySQL – Mchl Aug 10 '11 at 21:57
  • Indeed, that would be it. Good eyes :) – TML Aug 10 '11 at 21:58
0

mysqli_prepare() returns a statement object or FALSE if an error occurred.

TML
  • 12,813
  • 3
  • 38
  • 45