Jump to content

last record added


samsbc12

Recommended Posts

basically what i have done is I have a latests news box on the side of my site

I can add an entry but I want to recall the last one entered and display the info

<?
include("dbinfo.inc.php");
$result = mysql_query("SELECT FROM LatestNews  WHERE ID = MAX(ID);") //Pulls contact submissions
or die(mysql_error()); 
while($row = mysql_fetch_array( $result )) {
  echo "<h2> ";
  echo $row['date'];
echo "</h2>";
    echo "<p>";
  echo $row['message'];
  echo "</p>";
  }
  mysql_close();
?>

Gives me
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM LatestNews WHERE ID = MAX(ID)' at line 1

I have no idea what i am doing

I am using sql version Client API version  4.0.27 

Aslo a side note when ever i echo a variable in a double qouted string i get errors any ideas

Link to comment
https://forums.phpfreaks.com/topic/31844-last-record-added/
Share on other sites

Here are two ways to do it:

[code=php:0]SELECT * FROM LatestNews ORDER BY ID DESC LIMIT 1[/code]


[code=php:0]SELECT * FROM LatestNews WHERE ID = (SELECT MAX(ID) FROM LatestNews)[/code]


The first one is probably faster.  The reason your query doesn't work (apart from missing the "*" after select) is that you cannot use max() as a condition unless you are also using group by.  You can use max() as the result of the query anytime, but not as a condition.
Link to comment
https://forums.phpfreaks.com/topic/31844-last-record-added/#findComment-147761
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.