storing in arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

storing in arrays

Post 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));
}
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Re: storing in arrays

Post 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.
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: storing in arrays

Post 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
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Re: storing in arrays

Post 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.
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: storing in arrays

Post 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?
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Re: storing in arrays

Post 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.
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: storing in arrays

Post 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...
Post Reply