homepage Welcome to WebmasterWorld Guest from 54.205.247.203
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
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
LinusIT




msg:4426377
 8:39 am on Mar 8, 2012 (gmt 0)

Hi

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

 

IanKelley




msg:4426838
 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];

LinusIT




msg:4426976
 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