Ive followed a bunch of different examples regarding using a SELECT in a prepared statement, but nothing is returned. EDIT I have changed my code a bit to look like this:
$date1 = 2012-01-01;
$date2 = 2012-01-31;
$sql_con = new mysqli('db', 'username', 'password', 'database');
if($stmt = $sql_con->prepare("SELECT eventLogID FROM Country WHERE countryCode=? AND date BETWEEN ? AND ?")){
$stmt->bind_param("sss", $country_code, $date1,$date2);
$stmt->execute();
$i=0;
while ($stmt->fetch()){
$stmt->bind_result($row[$i]);
$i++;
}
$stmt->close();
$sql_con->close();
Now all the desired entries, except for the first, are added to $row[]. Why isnt the first entry being added? Thanks in advance!
I think you have to bind to the columns in bind_results() like
Here $col1 and $col2 binds to Code and Name columns of Country table
(Instead of * in SELECT use the column names)
Further reference : http://php.net/manual/en/mysqli-stmt.bind-result.php
EDIT 07/2015 (question has been edited since original answer but underlying principles are the same)
Never
SELECT *
in a production environment, it will only come back to bite you in weird, unpredictable and seemingly unrelated ways. By specifying the columns you want, you will ensure that column ordering, data-type, constraint and all sorts of other elements won't cause you problems in the long run.This answer is still mostly valid so I'll leave it here as-is, but the main take-away is: use PDO, it does 98% of the things you'll ever need with a much cleaner and more succinct API over the same back end. If you need a more complex RDBMS-specific API then you'll already understand the problems you have and why you need mysqli etc instead.
SELECT *
doesn't work very well with MySQLi prepared statements. It's one of the major reasons I recommend PDO instead - that and the ridiculous requirement to bind variable references instead of values to the parameters.This is not binding the result row to a variable, it would just be binding a single column. And because you have used
SELECT *
, it doesn't do what you want it to.If you do want to use MySQLi over PDO (which, as I say, I would recommend) there are a few good examples of how to
SELECT *
in the comments like this one on thebind_result()
manual page.Or you can just specify the columns you want to retrieve:
EDIT based on your new code, you should be doing this: