MemphiS Posted June 8, 2007 Share Posted June 8, 2007 IM trying to get cost values out of the db with a specific ID which is assigned to an item ID but i only get the last record in the database. $lala = mysql_query("SELECT `cost` FROM `table` WHERE `ID` = '1'"); while ($la = mysql_fetch_row($lala)){ $start = 0; $overall = $start+=$la[0]; } echo("$overall"); ... Quote Link to comment https://forums.phpfreaks.com/topic/54740-retreiving-all-records-from-sql-db/ Share on other sites More sharing options...
per1os Posted June 8, 2007 Share Posted June 8, 2007 $lala = mysql_query("SELECT `cost` FROM `table`"); // You were only selecting ONE ID, this will select all records. while ($la = mysql_fetch_row($lala)){ $start = 0; // so you are always adding zero ?? $overall += ($start + $la[0]); // you had the += in the wrong spot. } echo("$overall"); What is suppose to be housed in $start ??? Quote Link to comment https://forums.phpfreaks.com/topic/54740-retreiving-all-records-from-sql-db/#findComment-270777 Share on other sites More sharing options...
brissy_matty Posted June 8, 2007 Share Posted June 8, 2007 it's only displaying the last record because when you are "echoing" the output it is outside of the loop - so the loop has finished and its set on the last record. Least thats what i think... your code: $lala = mysql_query("SELECT `cost` FROM `table` WHERE `ID` = '1'"); while ($la = mysql_fetch_row($lala)){ $start = 0; $overall = $start+=$la[0]; } echo("$overall"); would prob be better as: <?php $lala = mysql_query("SELECT `cost` FROM `table` WHERE `ID` = '1'"); $result=mysql_query($lala); $num=mysql_numrows($result); mysql_close(); $i=0; while ($i < $num) { $cost=mysql_result($result,$i,"cost"); echo "$cost<br>"; // If you need to add a zero to the front then just add a 0 before $cost $i++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54740-retreiving-all-records-from-sql-db/#findComment-270791 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.