Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble executing a SELECT query in a prepared statement

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!

like image 874
Glenncito Avatar asked Jul 20 '12 08:07

Glenncito


1 Answers

Not at all a fan of "bind_result" in mysqli beyond the simplest one-row-expected queries.

Stuffing a whole row into a single array item is better done with:

$stmt->execute();
$result = $stmt->get_result();
while ($data = $result->fetch_assoc())
    {
        $retvar[] = $data;
    }
$stmt->close();

or

while ($data = $result->fetch_row())

if you don't want/need the field names.

like image 171
Roger Krueger Avatar answered Oct 13 '22 00:10

Roger Krueger