2

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.

5
  • What data type is item in the database? Commented Aug 8, 2016 at 8:47
  • bro item is INT type Commented Aug 8, 2016 at 9:05
  • "SELECT MAX(item) AS result FROM product WHERE email = '$emailid' AND username = 'arun'" try this Commented Aug 8, 2016 at 9:07
  • $sql = "select item from product where email = '$emailid' AND username = 'arun' order by item desc limit 1"; ? Commented Aug 8, 2016 at 9:18
  • Possible duplicate of SELECT MAX(... not returning anything in PHP/MYSQL Commented Aug 8, 2016 at 9:20

3 Answers 3

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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'];

Comments

0

Try this way,

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

Comments

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.