PDA

View Full Version : indexed array from recordset


CraigR
06-27-2011, 05:01 AM
I am trying to create a simple, indexed array from a recordset, but cannot seem to get it in the correct format.

I am retrieving a single field, EMail.

If I was to do this manually, it would look like

$emailarray = array('name1@address.com', 'name2@address.com','name3@address.com');and if i did a print_r($emailarray);

it would look like

Array ( [0] => name1@address.com [1] => name2@address.com [2] => name3@address.com ) which is exactly what i want

when i try and do the same using a recordset, i cannot get the same result.

If I try

$emailarray = array();
do {
$emailarray[] = $row_rsmembersreplied;
} while ($row_rsmembersreplied = mysql_fetch_row($rsmembersreplied));
print_r($emailarray);i get

Array ( [0] => Array ( [email] => name1@address.com ) [1] => Array ( [0] => name2@address.com ) [2] => Array ( [0] => name3@address.com ) )

If I omit the [] within the do..while loop. i get the correct format, but only one row returned

Jason Byrnes
06-27-2011, 08:39 AM
try changing:
$emailarray[] = $row_rsmembersreplied;

to:
$emailarray[] = $row_rsmembersreplied['email'];



just using $row_rsmembersreplied is returning an array of the column name => column value

CraigR
06-28-2011, 07:10 AM
Hi Jason,

just got around to trying this, if I make the change as suggested i get

Undefined index error

i have attached a version of the page - thanks

Jason Byrnes
06-28-2011, 07:42 AM
then try using:
$emailarray[] = $row_rsmembersreplied['MemberEmail'];

and dont cast the MemberEmail to an alias in the SQL:
SELECT MemberEmail AS email

CraigR
06-28-2011, 10:10 AM
doesn't make any difference, i tried it with and without the alias

CraigR
06-28-2011, 10:13 AM
hi Jason.

fixed it.

my line should have read mysql_fetch_assoc($rsmembersreplied));

whereas it read

mysql_fetch_row($rsmembersreplied));

thanks for your help

Jason Byrnes
06-28-2011, 10:41 AM
glad to hear it is working.