0

i have a mysql table the contains an index id, a data entry and 10 other columns called peso1, peso2, peso3...peso10. im trying to get the last 7 peso1 values for a specific id. like:

    $sql = mysql_query("SELECT * FROM table WHERE id='$id_a' ORDER BY data DESC LIMIT 0, 7");

when i try to fetch those values with mysql_fetch_array, i get all values together.

example:

    while($line = mysql_fetch_array($sql)) {

echo $line['peso1'];
    }

i get all peso1 values from all 7 days together. How can i get it separated?

1
  • what is the output and what is desired? Commented Jun 8, 2014 at 17:39

2 Answers 2

1

They will appear all together because you are not separating them as you loop through them.

for example, insert a line break and you will see them on separate lines

while($line = mysql_fetch_array($sql)) {
    echo $line['peso1'] ."<br />";
}

You could key it as an array like so

$myArray = array();
$i = 1;

while($line = mysql_fetch_array($sql)) {
        $myArray['day'.$i] = $line['peso1'];
        $i++;
}

Example use

$myArray['day1'] // returns day one value
$myArray['day2'] // returns day two value
Sign up to request clarification or add additional context in comments.

Comments

0

It's not clear what you mean by "separated" so I'm going to assume you want the values as an array. Simply push each row field that you want within your while loop onto an array like this:

$arr = array();
while($line = mysql_fetch_array($sql)) {
  $arr[]=$line['peso1'];
}
print_r($arr);//will show your peso1 values as individual array elements

2 Comments

thanks. i want to be able to get each element of this new array. how can i do that?
in the case above you would reference the array index, so for example if you wanted the 3rd array element you would reference it like this: $arr[2]. accessing array elements is too broad a topic for me to give you specific advice without knowing the intricacies of your objectives.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.