Jump to content

Recommended Posts

Ok so if I have a table set out like this

 

id

number 1

number 2

number 3

 

I'd want to locate data from the row where id = 3, but retrieve information from the column with the highest value in it, ie number 1, 2 or 3.

 

Is this possible? or do i need to retrieve all the data then use a set of nested ifs to find the highest?

<?php

$sql = mysql_query("select * from table where username = 'user1'")
while($r=mysql_fetch_array($sql)) {
     $var1 = $r["var1"];
     $var2 = $r["var2"];
     $var3 = $r["var3"];
}
?>

 

Not sure how to insert value + variable name into array

 

then how to sort into order, then retrieve the 3 highest?

 

something to do with array compact? I dont really use arrays much

I really think those who can't be bothered to normalize their data deserve to struggle, but here

 

$sql = "SELECT a.number1, a.number2, a.number3 
        FROM andrewgarn a
        WHERE id = 1";
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
$hival = max($row);                            // find highest value
$colname = array_search($hival, $row);         // find it's column name

echo "$hival : $colname";

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.