[SOLVED] from db into 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
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

[SOLVED] from db into array

Post by rsmarsha »

I want to select a set of id's from a db with a query, which i have. Once selected i need them put into an array which can then be used to loop through. I can create an array to loop through myself, but can't seem to create one using a result set from the db. Any ideas?
Last edited by rsmarsha on Wed Dec 14, 2005 4:04 am, edited 1 time in total.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

$array=array();

then....

Code: Select all

$i=0;
while ($test=mysql_fetch_object($myqueryresult))
{
 $array[i]=$test->id;
$i++;
}
and that should make your populated array....then to loop through it immediatly afterwards

Code: Select all

for($L=0;$L<$i;$L++)
{
 echo $array[L];
}
tada?:-D
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

Got this so far.

Code: Select all

if ($_POST['type']=='pc')
{
$set = "SELECT cat_id FROM categories WHERE cat_type!='lp'";
$sq = mysql_query($set) or die("Query $set Failed".mysql_error());
}
else
{
$set = "SELECT cat_id FROM categories WHERE cat_type!='pc'";
$sq = mysql_query($set) or die("Query $set Failed".mysql_error());
}
$array=array();
$i=0; 
while ($type_row=mysql_fetch_object($sq)) 
{ 
$array[i]=$type_row->cat_id; 
$i++; 
}
for($L=0;$L<$i;$L++) 
{ 
echo 'test: '.$array[L].''; 
}
Doesn't seem to echo the id's.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

this line...

Code: Select all

$array[i]=$type_row->cat_id;
...should be...

Code: Select all

$array[$i]=$type_row->cat_id;
and this line...

Code: Select all

echo 'test: '.$array[L].'';
...should be...

Code: Select all

echo 'test: '.$array[$L].'';
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

Thanks :) Seems to be working. :)
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

sorry.that's my c++ coming through... :oops:
Post Reply