Solved

MySQL Error

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 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