I'm writing a section of code using PHP which gets the last 5 newest news posts from my MySQL database. The table description is this:
n_id -> int(6) unsigned, not null, auto_increment, primary key (used as a unique ID for each news post)
date -> varchar(8) not null (used to store the date of the entry which is sent with the PHP script when the post is added to the db)
heading -> varchar(40) not null (stores the heading of the news post)
n_post -> varchar(255) not null (stores the actual news post)
my current PHP is set up like this:
...I haven't finished it yet but I'm wondering something...lets say that there is only 1 news post in the database, I'm going to get an error when trying to select 5 rows, correct? how can I prevent this? I want to be able to determine if there is 5 rows, and if so, select the 5 newest...am I going to get an error when trying to order the date column and its a varchar? how can I make sure I get the 5 newest posts?
n_id -> int(6) unsigned, not null, auto_increment, primary key (used as a unique ID for each news post)
date -> varchar(8) not null (used to store the date of the entry which is sent with the PHP script when the post is added to the db)
heading -> varchar(40) not null (stores the heading of the news post)
n_post -> varchar(255) not null (stores the actual news post)
my current PHP is set up like this:
Code:
$news = mysql_connect("localhost","newseditor","n3ws3dit0r");
if (!$news)
{
echo "Error: Could not connect to database for news: ".mysql_error()."<br />";
exit;
}
mysql_select_db("archied");
$last_five_posts=mysql_query("select date,heading,n_post from news order by date limit 5");
echo "Error with news query: ".mysql_error()."<br />";