Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.