Jump to content


Photo

Query results to an array


  • Please log in to reply
4 replies to this topic

#1 cleibesouza

cleibesouza
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 04 April 2006 - 09:35 PM

This is got to be pretty simple, still I can figure it out.
How can I dump the results of a query into an array so I can separate the results and use them on another query?

Below is my code

SELECT activities_id 
FROM participation 
WHERE users_id = '$userID'";

$participationResults = mysql_query($participationQuery, $connection) or die (mysql_error());
$partNumResults = mysql_num_rows($participationResults);

if($partNumResults)
{
  for($y = 0; $y < $partNumResults; $y++)
 {
    $row = mysql_fetch_array($participationResults);
    echo $row['activities_id'];
 }//end for
}//end if

Now I want to use the results of this query into another query or manipulate it like, let's say if the query results are 2, 10 and 27. I'd like to be able to use only 2 or only 10 and so on.
I understand that the mysql_fetch_array returns the results in an array but can't figure out how to separate them.
My thought was to have them on an array to be able to use them. I'm open to any suggetions.

Thanks.

#2 DrDre

DrDre
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 04 April 2006 - 09:44 PM

Confused on what data are you wanting to use? The values from each column?
ID | ACT | VAR
2   | 10   |  27

Is that the 2,10,27 you mean? Or
ID   | ACT | VAR
      |   2    |
      |   10  |
      |   27  |
To use the 2nd way youll need a
while ($row = mysql_fetch_array($partresults)) {
echo $row['activity_id'];
}

[a href="http://forums.fanetworks.net" target="_blank"][/a]
» faNetworks Webmaster. [[a href="http://fanetworks.net" target="_blank"]fanetworks.net[/a]]
» faSoftware Developer [[a href="http://software.fanetworks.net" target="_blank"]software.fanetworks.net[/a]]
» faForums - php Resource + more [[a href="http://forums.fanetworks.net" target="_blank"]forums.fanetworks.net[/a]]
» Experienced php Coder - Ill help anyone I can if I have time ;)

#3 cleibesouza

cleibesouza
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 04 April 2006 - 09:51 PM

[!--quoteo(post=361743:date=Apr 4 2006, 05:44 PM:name=DrDre)--][div class=\'quotetop\']QUOTE(DrDre @ Apr 4 2006, 05:44 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Confused on what data are you wanting to use? The values from each column?
ID | ACT | VAR
2   | 10   |  27

Is that the 2,10,27 you mean? Or
ID   | ACT | VAR
      |   2    |
      |   10  |
      |   27  |
To use the 2nd way youll need a
while ($row = mysql_fetch_array($partresults)) {
echo $row['activity_id'];
}
[/quote]

The values 2, 10 and 27 are different rows under the activities_id column. This is the column I need to use on a different query.


#4 DrDre

DrDre
  • Members
  • PipPipPip
  • Advanced Member
  • 35 posts

Posted 04 April 2006 - 09:57 PM

$participationResults = mysql_query($participationQuery, $connection) or die (mysql_error());
$id=0;
while($row = mysql_fetch_array($participationResults)) {
    $results[$id] = $row['activities_id'];
    $id++;
}


$results will be an array of all your results now
[a href="http://forums.fanetworks.net" target="_blank"][/a]
» faNetworks Webmaster. [[a href="http://fanetworks.net" target="_blank"]fanetworks.net[/a]]
» faSoftware Developer [[a href="http://software.fanetworks.net" target="_blank"]software.fanetworks.net[/a]]
» faForums - php Resource + more [[a href="http://forums.fanetworks.net" target="_blank"]forums.fanetworks.net[/a]]
» Experienced php Coder - Ill help anyone I can if I have time ;)

#5 cleibesouza

cleibesouza
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 04 April 2006 - 10:04 PM

Getting this warning now:
Warning: Cannot use a scalar value as an array in [filepath] on line [line #]






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users