Query results into an array only storing one result

I'm successfully connecting to a mssql database and getting results, but I'm trying to place them in an array for display on the page.

The following always gets 1 result and nothing more.
I thought arrays are supposed to store multiple results?

<?php

$conn
=odbc_connect('*****','******','*****');
if (!
$conn)
  {exit(
"Connection Failed: " . $conn);}

$sql="select Name,Id,Group from members";
$rs=odbc_exec($conn,$sql);
if (!
$rs)
  {exit(
"Error in SQL");
echo
'<div>';

while (
odbc_fetch_row($rs))
  {
 
$id=odbc_result($rs,"Id");
 
$group=odbc_result($rs,"Group");
 
$name=odbc_result($rs,"Name");

 
$memberinfo = array("group" => "$group", "Name" => "$name", "Id" => $id);

  }
 
var_dump($memberinfo);
odbc_close($conn);
echo
"</div>";
?>

What am I doing wrong here?

Your rating: None Average: 5 (1 vote)

Replies

using arrays

You're over writing the prior value on each loop through your record set.

Try this instead

  $memberinfo[] = array("group" => "$group", "Name" => "$name", "Id" => $id);

Which will create an array for each result

$memberinfo[0] first result and so on depending on how many rows returned.

Your rating: None

Post new comment

 
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><code>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters (without spaces) shown in the image.