Database Query to populate an array()

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
User avatar
dyluck
Forum Commoner
Posts: 54
Joined: Thu Jun 26, 2008 1:44 pm

Database Query to populate an array()

Post by dyluck »

Ok here's another one from me! I'm learning an unbelievable amount!

So what I would like to do is populate an array() or something of the sort by a database query

example shown below:

Code: Select all

$sql='SELECT * from streams JOIN link ON link.streamid=streams.streamid WHERE streams.streamtype = 2 AND link.uid = "'.$uid.'" AND link.sticky <> "" ORDER BY streams.streamid ASC';
$result=mysql_query($sql) or die("Query failed " . mysql_error());
$counter = 0;
while($row=mysql_fetch_assoc($result)) {
[b]$streamno = array($row['streamid']);[/b]
//rest of while loop
 
What I need to do is populate an array to be used in a "for each loop" that will be used for another database query. Can you do a database query to act like a while loop in a for each loop?

So I want to do something like this later on in the code:

Code: Select all

[b]foreach ($streamno as $stream)[/b] {
$sql='SELECT * from streams where streamtype = 2  AND streamid <> "'.[b]$stream[/b].'" ORDER BY streamid ASC';
$result=mysql_query($sql) or die("Query failed " . mysql_error());
$counter = 0;
while($row=mysql_fetch_assoc($result)) {
//rest of while loop
 
What happens is, the array only stores the first result but doesn't store all the results. I am sure I can get this to work if I can figure out how to populate that array with all the results. Maybe I'm using the wrong function (which is very possible knowing me).

Thanks!!! :)
User avatar
dyluck
Forum Commoner
Posts: 54
Joined: Thu Jun 26, 2008 1:44 pm

Re: Database Query to populate an array()

Post by dyluck »

Never mind... managed to figure this one out on my own

Stuff an array by a mysql query:

Code: Select all

$streamno = array();
$sql='SELECT * from streams JOIN link ON link.streamid=streams.streamid WHERE streams.streamtype = 2 AND link.uid = "'.$uid.'" AND link.sticky <> "" ORDER BY streams.streamid ASC';
$result=mysql_query($sql) or die("Query failed " . mysql_error());
while ($streamrow = mysql_fetch_array($result)) {
$streamno[] = $streamrow['streamid'];
}
use later:

Code: Select all

foreach ($streamno as $stream) {
//echo array
echo $stream.'<br>';
}
User avatar
dyluck
Forum Commoner
Posts: 54
Joined: Thu Jun 26, 2008 1:44 pm

Re: Database Query to populate an array()

Post by dyluck »

For those watching by the way... the rest goes like this

You have to separate the array string and make it "mysql" friendly for the array. In this case, I want everything except what was in my original array. :)

Code: Select all

foreach($streamno AS $key => $value){$temp[] = "'".$value."'";} 
$streamexclude = implode(",",$temp); 
$sql='SELECT * from streams where streamtype = 2  AND streamid NOT IN ('.$streamexclude.') ORDER BY streamid ASC';
$result=mysql_query($sql) or die("Query failed " . mysql_error());
$counter = 0;
Post Reply