homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Select Multiple Columns based on Max Value

Msg#: 4426375 posted 8:39 am on Mar 8, 2012 (gmt 0)


I have a database with around 50 columns, 1 column is "name" and the other columns are "item1, item2, item3" and so on.

I've got the following script to select the highest value from the item columns:

$sql1 = "SELECT max(item1), name FROM pricelist";
$result1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($result1);
$item1_1 = $row1['max(item1)'];
$item1_2 = $row1['name'];

It works fine but it isn't selecting the correct name.. I think it's because I'm not telling it which name to actually select.

Hopefully someone can help..



WebmasterWorld Senior Member 10+ Year Member

Msg#: 4426375 posted 7:15 am on Mar 9, 2012 (gmt 0)

Try this instead (untested):
$max = mysql_fetch_row(mysql_query("SELECT max(item1) FROM pricelist"));
$sql1 = "SELECT name FROM pricelist WHERE item1 = '{$max[0]}'";
$result1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_row($result1);
$item1_1 = $max[0];
$item1_2 = $row1[0];


Msg#: 4426375 posted 1:48 pm on Mar 9, 2012 (gmt 0)

Works perfect, thanks very much.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved