简体   繁体   中英

how to get max value of a sql database query in php

I want to find out the max value of sql data column on the basis of email id and username. (there is no primary entity) To get the email id, i store the user email id from session.

here goes my code:

$emailid = $userRow['emailid'];

$sql = "select max(item) from product where email = '$emailid' AND username = 'arun'";

$result = $conn-> query($sql);

$row = $result->fetch_assoc();
echo "Max item : " .$row['result'];

It's giving me first value of sql table but not highest.

you can either change your column data-type or use CAST or CONVERT .

$sql = "SELECT MAX( CAST( `item` AS UNSIGNED) ) as max FROM `product` WHERE `email` = '$emailid' AND `username` = 'arun'";

If possible its better to change the data-type.

Try this,

$emailid = $userRow['emailid'];
$sql = "SELECT MAX(item) AS item FROM product WHERE email = '$emailid' AND username = 'arun'";
$result = $conn-> query($sql);
$row = $result->fetch_assoc();
echo "Max item : " .$row['item'];

Try this way,

$rowSQL = mysql_query( "SELECT MAX( ID ) AS max FROM `tableName`;" );
$row = mysql_fetch_array( $rowSQL );
$largestNumber = $row['max'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM