views:

36

answers:

3

Ok so this one is strange and I'm thinking there must be a problem in my sql query but I don't see anything wrong with it. What is happening is that I have a script that I am running in a php script that retrieves all categories that a user has signed up for as well as how many other people have signed up for the same category. When I run the query in the php page it is very slow but does return results. I wanted to see what was happening so I went to phpmyadmin to run the same script and it returns the count of the results very fast but then gives me the following error where the results would normally be displayed:

#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 '' at line 1 

Here is the query:

SELECT t1.category_id, t3.description AS 'Category',
    t1.list_type_id, t2.name, t1.status_id, t2.user_id,
    t2.email, UNIX_TIMESTAMP( t1.record_date ) AS 'RecordDate',
    (
        SELECT COUNT( category_id ) 
        FROM t1_sub
        WHERE t1_sub = t1.job_ctg_id AND
            t1_sub.list_type_id = t1.list_type_id AND
            t1_sub.status_id =44
    ) AS 'MatchingListings'
FROM t1
INNER JOIN t2 ON t2.id_rfp_vendor_data = t1.user_id
INNER JOIN t3 ON t3.type_lookup_id = t1.job_ctg_id
WHERE t1.status_id =43

I am at a loss. The reason for my question is really two fold:

  1. Is there actually something wrong with my query that I am missing?
  2. If not, is the error mysql showing the reason why my script is running slow and that php has some way around this? (Though this seems like an unlikely possibility I just have to ask to be sure)

Thanks!

A: 

Do you have spaces in any of your table or column names? If so, enclose them in backticks:

`table name`
Scott Saunders
@Scott no spaces. Personally I feel like if you have spaces in your table or column names you should be shot. Again just my opinion though. :P
spinon
@spinon: I agree about the spaces. I'm just not seeing anything weird on line 1 except for the 't1', etc. which I assume are not the real table names.
Scott Saunders
Table names aren't keywords are they?
Scott Saunders
@Scott no table names are not keywords. They are pretty hideous table names and there is no way they could be keywords.
spinon
+1  A: 

PHPMyAdmin takes the liberty to slightly rewrite your queries (for example, adds the LIMIT 0,30 clause as to only show the first 30 lines).

Can you enable the query-log and see which query actually gets executed on the server?

If not, try removing elements from the query until it does work. Show us that query. Or show some table definitions?

Konerak
@Konerak in this instance no extra code is being added as I have checked it. Let me see about the query log and see what that shows? After that I will try adding some table definitions.
spinon
@Konerak I removed the count subquery and it turns out that is the problem. But it seems pretty basic.
spinon
@Konerak Ok another find is that if I remove all the labels for the columns then that works.
spinon
It works not without changing a thing. I don't know what happened. It must have been a problem within mysql that was temporary.
spinon
A: 

What you should do is delete phpmyadmin and promise never to download it again.

Then fire up a command-line mysql prompt, and try the same query. This will tell you whether the query is genuinely bad, or if phpmyadmin was simply trashing it for the perverted pleasure of its misguided authors.

MarkR
@Mark I do agree somedays. Though phpmyadmin is great for letting me be lazy. Sometimes I don't always want to work with the command line. Great when I want that granular control but not great when I don't want to type a lot. Which on a Friday @ 3 the latter is definitely the case.
spinon
@MarkR: this intrigues me. What don't you like about PHPMyAdmin?
Konerak
I have many times read stories where people lose either data or time to phpmyadmin malfunctioning; to me this is just too much of a risk. It may be ok for readonly operations.
MarkR