Select the maximum value of a colum in MySQL

Total Post:13

Points:95
 1050  View(s)
Ratings:
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)
  1. Post:397

    Points:3117
    Re: Select the maximum value of a colum in MySQL

    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

Answer