Solved

MySQL Error

Posted on 2016-10-23
3
39 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
ID: 41856040
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
ID: 41856045
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
ID: 41856109
Thank you Ray.  Enjoy your Sunday!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

920 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

15 Experts available now in Live!

Get 1:1 Help Now