php - Getting newest newspost database
I'm trying to get the newest post from my Database. If I run the following code I get the following message:
"Invalid query: 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 'TABLE newsposts ORDER BY newsposts.post_id DESC ' at line 2"
<?php
$getPosts = mysql_query("SELECT newsposts.post_id, newsposts.post
FROM TABLE newsposts
ORDER BY newsposts.post_id DESC
LIMIT 1") or die('Invalid query: ' . mysql_error());
while($getPosts_Array = mysql_fetch_array($getPosts)){
$post_id = $getPosts_Array['post_id'];
$post = $getPosts_Array['post'];
echo "
$post;
";
}
?>
Answer
Solution:
get rid of "TABLE" in your SQL statement as the
FROM
clause assumes a Table.Answer
Solution:
First off, MySQL_* functions are deprecated and you should be using MySQLi or PDO_MySQL
Using PDO, grabbing the latest post is simple:
Answer
Solution:
Your query should be: