Solved

MySQL Error

Posted on 2016-10-23
3
34 Views
Last Modified: 2016-10-23
I am getting an error on Line 1....can't seem to figure it out.

$result = $db->query("SELECT `views`.`track`,`tracks`.`title`,`tracks`.`art`, COUNT(`track`) as `count` FROM `views`,`tracks` WHERE `views`.`track` = `tracks`.`id` AND DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= date(`views`.`time`) AND `tracks`.`public` = 1 AND `art` != 'default.png' GROUP BY `track` ORDER BY `count` DESC LIMIT 20");
[Err] 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 '$result = $db->query("SELECT `views`.`track`,`tracks`.`title`,`tracks`.`art`, CO' at line 1

Open in new window

0
Comment
Question by:DS928
  • 2
3 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
It might be "ORDER BY count" since "count" is a reserved function name.  Maybe try changing that to "kount" or something like that?  I'll try a few experiments and post back in a while.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
Try the query like this.  Note that I have prepared the query in a PHP variable, instead of a part of the function call.  This makes error visualization much easier, because you can print a PHP variable and look at its contents.  You can't do that if your query string is constructed inside the function arguments.  Also, if you separate the query into different lines, the error messages will give you a line number associated with the syntax error.  Much easier to spot errors this way, than if the entire query is all on one line!

I can't test it because I do not have your database, but the error I got when I ran it was related to the missing table, so I think the syntax might be OK now (I hope).
$sql
=
"
SELECT
  views.track
, tracks.title
, tracks.art
, COUNT(track) AS kount
FROM
  views
, tracks
WHERE
  views.track = tracks.id
AND
  DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= DATE(views.time)
AND
  tracks.public = 1
AND
  art != 'default.png'
GROUP BY track
ORDER BY kount DESC
LIMIT 20
"
;

Open in new window

0
 

Author Closing Comment

by:DS928
Comment Utility
Thank you Ray.  Enjoy your Sunday!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
comparing strings that contain line breaks 18 34
Editing login page in zencart. 2 13
mysql left join sentence 7 19
sql sentence 2 10
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now