Error in my SQL syntax - Blog Tutorial

Post here is you are having problems with any of the tutorials.
Post Reply
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Error in my SQL syntax - Blog Tutorial

Post by nyo »

Hi,

I am following the tutorial about creating a blog and I am on part 3. I tested my SQL code in phpmyadmin just as you show in the video but it gave me this error:

#1064 - 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 '. 'post_id' AS 'id', 'posts' . 'post_title' AS 'title', LEFT('post' at line 2

And here is my code:
SELECT
	'posts' . 'post_id' AS 'id',
	'posts' . 'post_title' AS 'title',
	LEFT('posts' . 'post_body', 128) AS 'preview',
	DATE_FORMAT('posts' . 'post_date', '%d/%m/%Y %H:%i:%s') AS 'date',
	'comments' . 'total_comments',
	DATE_FORMAT('comments' . 'last_comment', '%d/%m/%Y %H:%i:%s') AS 'last_comment'
	FROM 'posts'
	LEFT JOIN (
	SELECT
		'post_id',
		COUNT('comment_id') AS 'total_comments',
		MAX('comment_date') AS 'last_comment'
		FROM 'comments'
		GROUP BY 'post_id'
) AS 'comments'
	ON 'posts' . 'post_id' = 'comments' . 'post_id'
	ORDER BY 'posts' . 'post_date' DESC
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Error in my SQL syntax - Blog Tutorial

Post by Tino »

You should use backticks ( ` ) around table and field names, whereas you are using single quotes ( ' ).

Why do so many people do this wrong?
Please check out my CodeCanyon items.
JelvinJS7
Posts: 341
Joined: Thu May 12, 2011 8:40 pm

Re: Error in my SQL syntax - Blog Tutorial

Post by JelvinJS7 »

Tino wrote:Why do so many people do this wrong?
Probably cuz single quotes are used in just about everything else in php, and this is the only time you see backticks. It males sense to an inexperienced coder that you use single quotes there.
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Error in my SQL syntax - Blog Tutorial

Post by Tino »

Well sure. But if you view it in full screen HD, it's quite easy to see the difference :?

Oh well.
Please check out my CodeCanyon items.
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Error in my SQL syntax - Blog Tutorial

Post by nyo »

Tino wrote:You should use backticks ( ` ) around table and field names, whereas you are using single quotes ( ' ).

Why do so many people do this wrong?
I tried that as well before posting this, but it didn't work. I will try it once more to make sure.

EDIT: Ok, it worked now. Thanks a lot!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Error in my SQL syntax - Blog Tutorial

Post by jacek »

Using the large version of the player on youtube on my laptop screen I can see the difference pretty clearly.

I will try yo remember to mention it though ;)
Image
Post Reply