CPU
CPU

Reputation: 17

How can I find max value in SQL and read into PHP using PDO?

I am using PHP to create a website whereby a user inputs values into a new row on one page (values for Awinner, Arunner and so on) and on the next page those values are retrieved from the database. For this to work I need to find the most recent id which will always be the highest. What code can I put in between the hashtags below in order to do this?

$sql  = "SELECT Awinner, Arunner, Bwinner, Brunner, Cwinner, Crunner, Dwinner, Drunner FROM groupStage";
$sql .= " WHERE id = #the maximum id#";

Then after this, I need to put each value (Awinner, Arunner, Bwinner, etc.) into its own variable in PHP so that I can display them all separately on the page. How can I do this? Sorry for lack of detail here but I am a newbie to PHP and SQL and am working on an important school project.

Upvotes: 0

Views: 220

Answers (2)

user3783243
user3783243

Reputation: 5223

Don't use a where use an order by with a limit to only return the 1 row.

SELECT Awinner, Arunner, Bwinner, Brunner, Cwinner, Crunner, Dwinner, Drunner
FROM groupStage
order by `the maximum id` desc
limit 1

Upvotes: 1

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 31991

try like below by using subquery

"SELECT Awinner, Arunner, Bwinner, Brunner, Cwinner, Crunner, Dwinner, Drunner FROM groupStage where id= ( select max(id) from groupStage)"

Upvotes: 1

Related Questions