Archive blog page

He has: 388 posts

Joined: Apr 2005

Im haveing a little problem with my archive page for my blog here is the code

<?php
-----conect to database------
-----
select database------

$result = mysql_query("SELECT timestamp, id, title FROM php_blog WHERE FROM_UNIXTIME( timestamp, '%Y' ) = $year ORDER BY id DESC");

while (
$row = mysql_fetch_array($result))
{
$date = date("l F d Y",$row["timestamp"]);
$id = $row["id"];
$title = $row["title"];

echo
"$date<br><a href=\"journal.php?id=$id\">$title</a><br><br>";
}

?>

'

here is the error message
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/vhosts_111mb/eimblog.111mb.com/archive.php on line 19

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

If you test the query outside of the program, does it return any results?

He has: 388 posts

Joined: Apr 2005

no

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

Try changing that first line to this one and see if it gives you a more detailed error message:

<?php
$result
= mysql_query(\"SELECT timestamp, id, title FROM php_blog WHERE FROM_UNIXTIME( timestamp, '%Y' ) = $year ORDER BY id DESC\") or die(mysql_error());
?>

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

Also, is $year definitely set beforehand? If not, it could cause the problem.

He has: 388 posts

Joined: Apr 2005

i got this
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 'ORDER BY id DESC' at line 1

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

Yep, that means $year is likely not defined. Try echoing $year right before you assign $result.

He has: 388 posts

Joined: Apr 2005

o sorry years is not defined sorry for the bother.

He has: 388 posts

Joined: Apr 2005

this is what the year variable should be right
$year= date("Y");

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

Are you looking for the current year (ie 2005)? If so, that looks right to me.

Maybe you should post more of your code.

He has: 388 posts

Joined: Apr 2005

yes i am looking for the current year and thanks again for all your help

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.