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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.