Solved

MySQL Error

Posted on 2016-10-23
3
42 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 109

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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.htaccess 5 35
PHP & MySql - Turn Select Query Results into a Glyphicon 3 28
UPDATE query not working in mysqli php 8 50
Checkout Page Input Field not aligned 1 21
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…
This article discusses four methods for overlaying images in a container on a web page
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

785 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