Jump to content

Retreiving all records from sql db


MemphiS

Recommended Posts

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");

...

Link to comment
https://forums.phpfreaks.com/topic/54740-retreiving-all-records-from-sql-db/
Share on other sites

$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 ???

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++;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.