Click to See Complete Forum and Search --> : [RESOLVED] Displaying Last 4 values from a database
NickG21
09-11-2007, 10:38 AM
hey everyone, i am trying to figure out how to only display the last 4 or 5 values that have been inserted into a database. I am doing this with a database of products to sell and would like to display the "Recently Added" products on a mini sidebar. I understand how to query through and retrieve all the results but with the id being auto_incremented, after an entry is deleted the number keeps going so my table could look like
ID: Name Price
1 blah
2
3
6
7
23
676 etc...
in this scenario i would like to display the products with ID's "676, 23, 7 and 6" respectively.
I believe it will be something to do with Last_insert_id() but i do not know where to start with this?
any help would be greatly appreciated
thank you in advance
Nick
housey
09-11-2007, 10:56 AM
Use a SQL statement something like SELECT * FROM products ORDER BY id DESC LIMIT 4
edit:
this gives you the records in descending order and limits it to 4 records. So this should be the last 4 records, the records with the highest id's
as far as I'm aware Last_insert_id() only gives the last row id of the last inserted record (i.e. the last sql insert query). I may be wrong..
NickG21
09-11-2007, 11:13 AM
that makes sense but with the code i have setup now the redisplay of the values doesn't seem to work, here is the Code:
<?php
include("dbinfo.inc.php");
mysql_connect($host,$dbusername,$dbpassword);
@mysql_select_db($database) or die( "Unable to select database");
$query ="SELECT * FROM products ORDER BY id DESC LIMIT 4";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
for ($i = 0; $i < $num_results; $i++){
$row = mysql_fetch_array($result);
$id = $row['ProductID'];
$name = $row['ProdName'];
$picname = $row['ProdImage'];
$desc = $row['ProdDesc'];
$price = $row['ProdPrice'];
echo "<tr><td>" . $id . "</td><td>" .
$name . "</td><td>" .
$desc . "</td><td>" .
$price . "</td><td><img src=\"$picname\" height= \"125px\" width=\"125px\"></td></tr>";
}
mysql_close();
?>
it says that the mysql_num_rows() and mysql_fetch_array are not valid result resources
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/www/nickgirard.freehostia.com/recent.php on line 22
discorax
09-11-2007, 12:32 PM
I've had a lot of problems with MySQL queries in PHP because of the syntax...PHP is just really finicky sometimes (it's usually a good thing)
I'm not sure if this is the problem that you are having...but you could try this:
$query = 'SELECT * FROM `products` ORDER BY `id` DESC LIMIT 4 ';
otherwise you're code looks fine to me...
If you're still having problems I'd suggest doing a google search for your errors and see what you come up with. Maybe there is something simple that we're overlooking.
housey
09-11-2007, 01:17 PM
whats happening? what errors do you get?
Don't think you have to use quotes around the table and field name..
housey
09-11-2007, 01:22 PM
it says that the mysql_num_rows() and mysql_fetch_array are not valid result resources
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/www/nickgirard.freehostia.com/recent.php on line 22
sorry just seen the errors you posted. I would think that the table 'products' and or field name for 'id' is wrong as the SQL looks ok. Check the database for exact names you gave, its easy done. :)