Display recent added post.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jauson
Forum Contributor
Posts: 111
Joined: Wed Oct 05, 2011 12:59 am

Display recent added post.

Post by jauson »

Hi guys!

Need your help badly!

say there are 25 inserted index_id in my t_board, and I want to display the last index_id w/c index_id 25 in my view_form.
heres my code; view_form.php

==========================
$sql = "SELECT * from t_board LIMIT 0,1"; <---- what should I do or add to make my script decrement.
note: I only limit my form to display only 1.
==========================


Any help will be appreciated! :D
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Display recent added post.

Post by Celauran »

Code: Select all

SELECT col1, col2, col3
FROM tablename
WHERE whatever
ORDER BY id DESC
LIMIT 1
Might be faster to split it into two queries, though; one to get MAX(id) then another using that id in a WHERE clause. This might also be better suited to the SQL forum.
jauson
Forum Contributor
Posts: 111
Joined: Wed Oct 05, 2011 12:59 am

Re: Display recent added post.

Post by jauson »

heres my script $sql = "SELECT * from t_board index DESC LIMIT 1";
|
|
Notice: Undefined index: index in C:\xampp\htdocs\Lacson_exam\src\view.php on line 11. :(
thank you! :)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Display recent added post.

Post by Celauran »

What is 'index DESC'?
jauson
Forum Contributor
Posts: 111
Joined: Wed Oct 05, 2011 12:59 am

Re: Display recent added post.

Post by jauson »

heres my latest error now..
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Lacson_exam\src\view.php on line 26

script is working now but my next problem is my <? if($rows = mysql_fetch_array($result)) ?> etc...
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Display recent added post.

Post by Celauran »

We can't guess what your code is. It's going to be very difficult to help you without seeing it.

Code: Select all

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Lacson_exam\src\view.php on line 26
most likely means your query returned FALSE, meaning there's an error in the query.
jauson
Forum Contributor
Posts: 111
Joined: Wed Oct 05, 2011 12:59 am

Re: Display recent added post.

Post by jauson »

query not running well :(

$sql = "SELECT ( writer, subject, message, filename ) FROM t_board WHERE ORDER BY index DECS LIMIT 1";
$result = mysql_query($sql);
if (!$result){
echo "could not run query";
exit;
}

tell me is it correct?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Display recent added post.

Post by Celauran »

That query is definitely wrong. You haven't specified any criteria for WHERE; you'll either need WHERE field = value, or you'll need to remove the WHERE clause. 'ORDER BY index' is also likely wrong. Do you have a field called index? If so, since it's a reserved word, you'll need to enclose it in backticks (`). If not, you'll need to use a proper field name. DECS should be DESC.
jauson
Forum Contributor
Posts: 111
Joined: Wed Oct 05, 2011 12:59 am

Re: Display recent added post.

Post by jauson »

[SOLVED]

$sql = "SELECT * FROM `t_board` ORDER BY `index` DESC LIMIT 1"; thanks :)
Post Reply