Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result

Hi,

I have the following function to display posts on my site. It works fine on my localhost but when I uploaded the files to the server I got the following warning:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

I can’t see what I am doing wrong because it works just fine on localhost. Any ideas?

function get_posts() {
	$query = mysql_query("SELECT
				`post_title`,
				`post_name`,
				`post_body`,
				`post_date`,
				`author_title`,
				`author_name`
				FROM `posts`
				WHERE `post_type` = 'post'
				LEFT JOIN `authors` ON `author_id` = `post_author`
				ORDER BY `post_id` DESC LIMIT 0,8");
	while ($row = mysql_fetch_assoc($query)) { $posts[] = $row;}
	return $posts;
}

Instead of jumping straight in the array you need some error catching to see whats going wrong, see the below example.

$query = mysql_query("QUERY GOES HERE") or die(mysql_error());

I tried that, it says there is some kind of syntax problem in the query that I can’t spot. I can’t spot because it works just fine on my localhost.

Could you please post the error that you receive?

the error is invalid syntax

you can’t put the WHERE clause there

it goes after the FROM clause, and the FROM clause includes all joins

pro tip: test your queries outside of php first

It is solved now, thanks for your help Chris and Rudy.

Following was the error I got:

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 'LEFT JOIN authors ON author_id = post_author ORDER BY post_id

Thanks for the tip, I will definitely test my queries when I get such errors the next time. I now have the following and it works perfectly.

$query = mysql_query("SELECT 
                `post_title`, 
                `post_name`, 
                `post_body`, 
                `post_date`, 
                `author_title`, 
                `author_name`
                FROM `posts` 
                LEFT JOIN `authors` ON `author_id` = `post_author` 
                WHERE `post_type` = 'post'
                ORDER BY `post_id` DESC LIMIT 0,8");

I guess that my first query worked on the localhost and not on the server because they have different MySQL versions.

had to have been some other query

WHERE in front of LEFT JOIN has always been an error, in any mysql version, ever