Solved

MySQL Error

Posted on 2016-10-23
3
48 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 110

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 110

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
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…

707 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