Re: problem looping through records

From: Date: Sun, 10 Aug 2003 21:40:06 +0000
Subject: Re: problem looping through records
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-159061@lists.php.net to get a copy of this message
On Sun, 10 Aug 2003 22:11:09 +0100, you wrote: >I have a sql statement which brings about 15 records... inside the while >loop i am trying to get data from another table by passing an id, the >problem is i am only getting the first record .. not the whole 15 records.. > >any suggestion?? > >function list_ss_cats(){ >global $db; >$sql = "SELECT DISTINCT(new_cat) FROM logos WHERE format =3"; >$result = $db->sql_query($sql); >$num = $db->sql_numrows(); >while($row = $db->sql_fetchrow()){ > > $sql2 = "SELECT * FROM cats WHERE id='$row[new_cat]'"; Others have answered your immediate question, but it looks to me like you should be doing a join and only using one SQL statement instead of sixteen. Something along the lines of SELECT DISTINCT(logos.new_cat), cats.* FROM logos, cats WHERE logos.format = 3 AND logos.new_cat = cats.id (Though I'd remove the * personally, it can slow things down. Oh, and you get $num but never actually use it, and "$row1[en]" should be throwing a warning - it should be "$row1['en']").

« previous php.general (#159061) next »