问题描述:

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';

?>

网友答案:

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.

网友答案:

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

相关阅读:
Top