CaribouCode
CaribouCode

Reputation: 14398

Prepared statement & binding the results to array

I'm trying to fetch rows from a database and put them into an array but can't work it out!

So I fetch the data like so:

if ($archiveInfo = $mysqli->prepare('SELECT DATE,TITLE FROM Blog')) {
  $archiveInfo->execute();
  $archiveInfo->close();
}

But not sure exactly what is the best code to bind the results to an array. I'm guessing it would be a 2 dimensional array i.e. $archiveInfo[0]['date']

How to bind the array and also start echoing selected sections of the array?

Upvotes: 0

Views: 166

Answers (1)

skrilled
skrilled

Reputation: 5371

if ($archiveInfo = $mysqli->prepare('SELECT DATE,TITLE FROM Blog')) {
  $archiveInfo->execute();

   $archiveInfo->bind_result($date, $title);

    /* fetch values */
    while ($archiveInfo->fetch()) {
        echo $date." ".$title;
    }

  $archiveInfo->close();
}

you could also use http://php.net/manual/en/mysqli-result.fetch-array.php instead and loop through the array..

Upvotes: 1

Related Questions