Home > DeveloperSection > Forums > Select the maximum value of a colum in MySQL
Jennifer Morgan
Jennifer Morgan

Total Post:13

Points:95
Posted on    December-01-2015 12:53 AM

 MYSQL Database  PHP  MYSQL 
Ratings:


 1 Reply(s)
 353  View(s)
Rate this:
 All I want to do is select the maximum value of a column in MySQL. Like this:
$max = "SELECT MAX(Id) FROM trialtabel2"; 
$max1 =  mysqli_query($dblink, $max); 
echo= $max1;
My debugger is just saying that it is a query returning a 0 boolean value (false)


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Supported
Posted on    December-01-2015 1:14 AM

We need to fetch the data from the mysqli_result object that was returned to you when you executed your query using mysqli_query.
    $max = "SELECT MAX(Id) as id FROM trialtabel2"; 
    $max1 =  mysqli_query($dblink, $max); 
    $row = mysqli_fetch_assoc($max1);    // this was missing
    $id=$row['id'];
    echo $id;
Note: I removed the loop because with MAX query without any grouping you will get only 1 row returned. If you had multiple rows in result set you would need to loop through all of them

Don't want to miss updates? Please click the below button!

Follow MindStick