0

I have a database with the field Unique ID, which has two rows in it, both integers. The first row has value 3 and the second row 4. The aim of this script is to find the one with the highest value, but it only echo's "Unique ID".

Here is the code:

<?php
$connection = mysql_connect("localhost","root");

if(!$connection) {
die("Connection Failed" . mysql_error());
}
mysql_select_db("rewiredstate",$connection);
$max="SELECT MAX('Unique ID') as id FROM topics";
$maxquery= mysql_query($max) or die (mysql_error());
while($row = mysql_fetch_assoc($maxquery)) {
echo $row['id'];
}
?>

Does anyone have any ideas as to what might be wrong with it? Any help would be greatly appreciated. Thanks

1
  • Not an answer, but if this is new code I suggest you look into mysqli or PDO, because the mysql_ functions will be deprecated soon. Commented Aug 6, 2012 at 19:38

2 Answers 2

4

I think you need different quotes - ` rather than '

Sign up to request clarification or add additional context in comments.

Comments

0

Remove the quotes around Unique ID like this:

$max="SELECT MAX(Unique ID) as id FROM topics";

The mistake in your query is that, you have used wrong quote around the field name in MAX function. As per your query, the MAX function is given a string as parameter, which it compares with no real field values in the table. Thats why it returns Unique ID in the result.

You need to use MAX(field) OR you can choose not to use quote at all, like MAX(field). But certainly not MAX('field').

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.