frosty
frosty

Reputation: 2851

Prepared statements without using get_result()

My webserver does not have mysqlnd API extensions so I can't use it and would like to do the workaround.

Current gives error:

  $postid = $_GET['p'];

  $stmt = $conn->prepare('SELECT * FROM posts WHERE post_id = ?');
  $stmt->bind_param('s', $postid);

  $stmt->execute();

  $result = $stmt->get_result();

  while($postRows = $result->fetch_assoc()){
    $posts[] = $postRows;
  }

Workaround:

  $postid = $_GET['p'];

  $stmt = $conn->prepare('SELECT * FROM posts WHERE post_id = ?');
  $stmt->bind_param('s', $postid);

  $stmt->execute();

  $stmt->bind_result($post_id, $post_submitter_id, $post_title, ... [etc]);

  while($stmt->fetch()){
    $posts[] = $postRows; // ????
  }

How do I still create the array $posts in the new workaround?

Upvotes: 0

Views: 100

Answers (1)

Ethan22
Ethan22

Reputation: 747

http://php.net/manual/en/mysqli-stmt.bind-result.php

This link privides an example of looping through a result set. You could do something like this.

$stmt->bind_result($post_id, $post_submitter_id, $post_title, ... [etc]);

while($stmt->fetch())
{
     $posts[] = [
         'post_id' => $post_id,
         'post_submitter_id' => $post_submitter_id,
         ...
     ];
}

Upvotes: 1

Related Questions