MySQLi prepared statements
I've always used PDO instructions, but for some reason I can't convince the server guy to install PDO for php, but I have MySQLi, I have no idea what I am doing wrong, I am not getting a connection error, and I am not getting the request error no matter how I try to output it. That's what I'm doing.
include 'MySQLiConnect.php';
if($stmt = $mysqli->prepare("SELECT * FROM zipCodeTable WHERE zip_code = ?")){
$stmt->bind_param("s", '07110');
$stmt->execute();
$stmt->bind_result($resultsArray);
$stmt->fetch();
foreach($resultsArray as $columnData){
$matchingZipcode = $columnData['zip_code'];
$matchingTimezone = $columnData['time_zone'];
}
$stmt->close();
}
echo $matchingZipcode.', '.$matchingTimezone;
This is mostly just to confirm the zipcode of users who have never used the MySQLi prepared statements before, I tried to do it straight from the manual, not sure what I am doing wrong. Thanks for taking the time to read this.
source to share
You are trying to "bind" a literal string. You cannot do this. You have to bind the variable.
Edit
$stmt->bind_param("s", '07110');
For
$string = '07110';
$stmt->bind_param("s", $string);
Also, when you bind the result, you must provide a variable for each returned field.
For example:
$stmt->bind_result($zipCode, $timeZone);
This is a bit problematic in use SELECT *
. You might be interested to know this comment on how you can do this: http://www.php.net/manual/en/mysqli-stmt.bind-result.php#85470
source to share