Page 1 of 1

storing in arrays

Posted: Thu Sep 08, 2011 3:40 pm
by timoteo
Hi,
A seemingly simple problem has had my head burning today. Any ideas please...

this outputs the results (41, 48, 71, 72, 73, 20, 39, 41, 48, 50, 71, 72,) but I don't want to output to the screen. I want to store all the results in an array to compare with other result sets and use in another select statement. I haven't been able to store more than the first result in a variable and any arrays come out empty. Thanks for any help.

Code: Select all

 SELECT DISTINCT recoentryid FROM businessdetails WHERE category IN ('musician') AND country IN ('mx', 'qro')...(edited)
$result6 = mysql_query($query6, $recommendingpeople) or die(mysql_error());
$row6 = mysql_fetch_array($result6);
$rn6 = mysql_num_rows($result6);
if(isset($row6['recoentryid'])) {
do {echo $row6['recoentryid']; echo ", "; }
while($row6 = mysql_fetch_array($result6));
}

Re: storing in arrays

Posted: Thu Sep 08, 2011 3:49 pm
by ok
If you don't want to print the results ($row6['recoentryid']), just don't print them! (you are using the echo function inside the do...while loop)
Instead, create an array before the do...while loop and inside it store the results into the previously created array.

Re: storing in arrays

Posted: Thu Sep 08, 2011 3:55 pm
by timoteo
that is exactly what I want to do but I have spent hours looking at the php manual and I just can not figure out how to do this

Re: storing in arrays

Posted: Thu Sep 08, 2011 3:59 pm
by ok
The code should look something like this:

Code: Select all

$result6 = mysql_query($query6, $recommendingpeople) or die(mysql_error());
$row6 = mysql_fetch_array($result6);
$rn6 = mysql_num_rows($result6);

$results = array();
while ($row6 = mysql_fetch_array($result6)) {
    $results[] = $row6['recoentryid'];
}
The $results array should now contain a "recoentryid" on each element.

Re: storing in arrays

Posted: Thu Sep 08, 2011 4:10 pm
by timoteo
thanks for your help - still struggling...

my array is empty - I 'm sure I am creating a very basic mistake, however when I echo $results the result is 'array'

what am I doing wrong?

Re: storing in arrays

Posted: Thu Sep 08, 2011 4:16 pm
by ok
You can't echo an array, you can, however, use print_r or var_dump.

Please read the PHP manual to learn more about arrays.

Re: storing in arrays

Posted: Thu Sep 08, 2011 4:19 pm
by timoteo
peerrrfect - got it now - I spend hours studying that thing but on the wrong page. I knew it would be something simple but when every I tried to echo array thought it was empty - never clicked that you can't echo arrays. Sorry - learning it by myself. Anyway with help from great people like yourself I get it. thanks a lot...