I need to get the MAX ID and assign variables to some of the columns on that row. If you're using Mysqli, use [code ]mysqli::$insert_id[/code]. ... ('SELECT MAX(id) FROM `movies`')->fetchColumn(); Share on Social Networks: Twitter Facebook VK GooGle+. Select Maximum value. Plain php + mysql. The first releases with this fix will be: 4.1.11 and 5.0.3. If you're using PDO, use [code ]PDO::lastInsertId[/code]. La ventaja de este método con respecto al anterior es que no es necesario utilizarlo inmediatamente despues de utilizar la sentencia INSERT; ya que este método busca el id de mayor valor dentro de la tabla y como es auto incrementable el mayor siempre sera el ultimo. Imho it is expected behavior because MAX()/MIN() returns NULL if there were no matching rows. Simple and elegant solution to select max id of the table. Otra forma es mediante la sentencia “SELECT MAX(id) AS id FROM tabla”. The following query gives you next AUTO_INCREMENT value from the selected table which you can use to get the last id. I need a little help figuring out how to get the max id of a table. Hello Chen, Thank you for the report and test case. My Table has Column ID set to AUTO_INCREMENT and is the Primary Keyname. Hi, i want to run this query, and store the results in a php variable: mysql_query(SELECT MAX(Id) FROM `threads`); Change Color: Projects. MySQL - MAX Function - MySQL MAX function is used to find out the record with maximum value among a record set. Select a single row from the table in descending order and store the id.