0

I'm trying to get all the $title from a mysql table called pages.

But I do not understand why I can only get the last one using my code bellow:

    if( ! $stmt = $db_conx->prepare("SELECT id, title FROM pages") ) {
  die( $db_conx->error );
}

$stmt->bind_param('i', $id);
if ( ! $stmt->execute() ) {
  die( $stmt->error );
}
$stmt->bind_result($id, $title);
$stmt->store_result();

while($stmt->fetch()) {
    $value[] = array('id'=>$id, 'title'=>$title);
}

echo $title;

is there anything missing from my code?

I'm absolutely baffled with this. any help would be appreciated.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
user3592614
  • 99
  • 3
  • 13

1 Answers1

0

Your code seems to echo out the variable $title but you are in fact appending the results of your query to $value.

If you replace

echo $title;

with

print_r($value);

I am pretty sure that you will get the information you are looking for.

Your code appends elements to an array. Print_r is a quick nasty way of checking what is in an array (or other) but you will probably want to use a control structure like a foreach to get through the results in a meaningful manner:

foreach($value as $val)
{
    echo "ID: ".$val[0]." - Title: ".$val[1]."<br>\r\n";
}
Fluffeh
  • 33,228
  • 16
  • 67
  • 80