select maximum value of id from mysql table and save it as variable

mysql assign value to variable from select
php select max value from database
how to store select query result in variable in mysql
mysql set multiple variables from select
using variables in sql select statement
mysql set variable from select
select max into variable
mysql max

Is it possible to extract highest id from table (in this case 9) and return it as variable $maximum, which I can use later as integer?

You can use MAX() function. Doc can be found here

MySQL - Get a Max value from Column, then store in variable, MySQL uses := in SELECT to set values. So use this: Select @Var := (MAX(​Person_ID) + 1) From Person;. The use of = is just for a boolean  SELECT * FROM permlog WHERE id = ( SELECT MAX(id) FROM permlog ) ; This would return all rows with highest id, in case id column is not constrained to be unique.

Try this, It will must work.

$sql =  "SELECT id FROM some_table ORDER BY id DESC LIMIT 1"; 
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "id = " . $row["id"];
    }
} 

How to Get the Maximum Value of a Column of a MySQL Table , How to Get the Maximum Value of a Column of a MySQL Table Using PHP. PHP. In this article $result= mysql_query("SELECT MAX(column_name) AS maximum FROM table"); $row Then we query the database using the $result variable. The MySQL MAX() function is an aggregate function that returns the maximum value from an expression. Typically, the expression would be a range of values returned as separate rows in a column, and you can use this function to find the maximum value from the returned rows. If there are no matching rows, MAX() returns NULL.

You can try with following snippet:

$sql =  "SELECT * FROM some_table where id = (select max(id) from some_table)"; 
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "id = " . $row["id"];
    }
} 

Using User-Defined Variables in SQL Statements, Selection from MySQL Cookbook, 2nd Edition [Book] One way to do this is to first save the ID value in a variable, and then refer to the The following statement determines the highest sum of the arms and legs columns in the limbs table and  CREATE TABLE #temp1 ( id varchar(20) , rev int ) INSERT INTO #temp1 SELECT a.id, MAX(a.rev) as rev FROM ( SELECT id, content, SUM(rev) as rev FROM YourTable GROUP BY id, content ) as a GROUP BY a.id ORDER BY a.id I then joined these max values (#temp1) to all of the possible id/content combinations.

Try this, It will must work.

$sql =  "SELECT id FROM some_table ORDER BY id DESC LIMIT 1"; 
$result = $conn->query($sql);
$id = 0; 
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $id = $row["id"];
    }
} 

echo $id; 

Using SQL Variables in Queries, Using SQL Variables in Queries Problem You want to save a value from a query so mysql> SELECT @id := cust_id FROM customers WHERE cust_id=' customer determines the highest sum of the arms and legs columns in the limbs table  MySQL SELECT INTO multiple variables example. To store values from the select list into multiple variables, you separate variables by commas. For example, the following statement finds the city and country of the customer number 103 and stores the data in two corresponding variables @city and @country:

MySQL MAX() Function, The MAX() function returns the maximum value in a set of values. Note: See also the MIN() function. Syntax. MAX(expression). Parameter Values. Parameter  Introduction to MySQL MAX() function. The MySQL MAX() function returns the maximum value in a set of values. The MAX() function comes in handy in many cases such as finding the greatest number, the most expensive product, and the largest payment from customers. Here is the basic syntax of the MAX() function :

MySQL SELECT INTO Variable, This tutorial shows you how to use the MySQL SELECT INTO variable to store If the query return no rows, MySQL issues a warning of no data and the value of To ensure that the query always returns maximum one row, you use the LIMIT 1 We will use the customers table in the sample database for the demonstration. The following statement determines the highest sum of the arms and legs columns in the limbs table and assigns it to the @max_limbs variable: mysql>SELECT @max_limbs := MAX(arms+legs) FROM limbs; Another use for a variable is to save the result from LAST_INSERT_ID() after creating a new row in a table that has an AUTO_INCREMENT column:

mysql_insert_id - Manual, Predefined Variables · Predefined Exceptions · Predefined Interfaces and mysql_insert_id — Get the ID generated in the last query See also MySQL: choosing an API guide and related FAQ for more For more information about PHP's maximum integer values, please see the insert a new item into the database Select Rows with Maximum Value on a Column Example 2. In this example, we will show how to select rows with max value along with remaining columns. It is useful if you want to return the remaining columns (non-group by columns). For this SQL Server example, we used the Inner Join to join the employee table with itself.

Comments
  • Yes, user MAX() function of MySQL.
  • Hi, welcome to stack overflow. Please refer the How to Ask guide for more details on how to ask a question and update your question accordingly. Posting code as images is bad as it's not serchable, please add your code as formatted text
  • Further to @DDS comment, IMO you don't need to enclose a picture of your table... your question is rather simple and self-contained. You may want to add some code showing how you plan to use that variable value ... perhaps that would change the answer so slightly but that is only a plus.
  • I tried it that way, but every time I got 500 internal server error
  • Please share your php code for executing the query.
  • $result = mysql_query("SELECT MAX(id) FROM amx_amxadmins");
  • it works, but it outputs also this: id = 9Error: SELECT id FROM amx_amxadmins ORDER BY id DESC LIMIT 1
  • you can get it direct with id as well. if it works then mark it green tick :-)
  • if you need it into a variable then use this $id = 0; if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $id = $row["id"]; } } echo $id;
  • id = Error: SELECT MAX(id) FROM amx_amxadmins
  • it outputs the exact id, but also id = 9Error: SELECT * FROM amx_amxadmins where id = (select max(id) from amx_amxadmins) , how can I get rid of that error?
  • Can you share your code and the screen shot of the error you are getting.
  • Got it! Thank you.
  • @AdamRose Mark this answer green tick as accepted if it resolved your issue. Thanks