Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-07-2011, 09:31 AM   PM User | #1
schuhmi2
New to the CF scene

 
Join Date: Mar 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
schuhmi2 is an unknown quantity at this point
Question Query sometimes fails

Hi all,

I have a query, which when run from within phpMyAdmin, it works, however when integrated within a website written in Perl, it fails and throws errors.
Both are run with a connection to the same database, and from coding/formatting side when integrated into the Website, all is correct and the same as other queries.

Any help with this would be much appreciated - Thanks!

MySQL Query:

Code:
CREATE TEMPORARY TABLE tmp_lecture_days (
timeslot_id int(50)
);
INSERT INTO tmp_lecture_days (timeslot_id)
SELECT DISTINCT tab_appointment.timeslot_id
FROM tab_appointment WHERE lecture_id = '1115';
SELECT COUNT(timeslot_id)
FROM tmp_lecture_days;
Error Log:

Code:
7.3.2011   10:14:12   error        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 ';
7.3.2011   10:14:12   error             INSERT INTO tmp_lecture_days (timeslot_id)
7.3.2011   10:14:12   error             SELECT DISTINCT tab_appoi' at line 3

Last edited by schuhmi2; 03-08-2011 at 07:29 AM.. Reason: Resolved
schuhmi2 is offline   Reply With Quote
Old 03-07-2011, 11:20 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
7.3.2011 10:14:12 error 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 ';
print the sql statement in that file and check whether there's any error in the syntax.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 03-07-2011, 11:52 AM   PM User | #3
schuhmi2
New to the CF scene

 
Join Date: Mar 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
schuhmi2 is an unknown quantity at this point
Hi abduraooft,

This probably will sound (and probably is) a stupid question,
but what exactly do you mean?

I know it works in phpMyAdmin, its just when Its integrated into my perl site, it throws errors.

Perl Code is so:

Code:
				$query
					= &statement_database(
						"CREATE TEMPORARY TABLE tmp_lecture_days (
						timeslot_id int(50)
						);
						INSERT INTO tmp_lecture_days (timeslot_id)
						SELECT DISTINCT tab_appointment.timeslot_id
						FROM tab_appointment WHERE lecture_id = '1115';
						SELECT COUNT(timeslot_id)
						FROM tmp_lecture_days;");
						
				 my ($days) = $query->fetchrow_array;
The rest of my SQL is formatted exactly the same as this about, and all function.

Thanks,
schuhmi2 is offline   Reply With Quote
Old 03-07-2011, 04:29 PM   PM User | #4
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
What is the code in your statement_database subroutine?

You're passing 3 separate sql statements as a single string. How is your subroutine handling that string?
FishMonger is offline   Reply With Quote
Old 03-07-2011, 09:51 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Generally, with PHP, you can *NOT* send more than one query to the DB at a time. (Silly, but true; other libraries in Java and C++ and even ASP.NET have no problems with doing so.)

Try cutting that into three separate queries and see if the problem doesn't indeed go away.

And not to ask a dumb question, but WHY are you using 3 queries, in the first place???
Code:
SELECT COUNT(DISTINCT timeslot_id)
FROM tab_appointment WHERE lecture_id = '1115';
Were you perhaps not aware that MySQL supports COUNT( DISTINCT xxx)?

http://dev.mysql.com/doc/refman/5.1/...count-distinct
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
schuhmi2 (03-08-2011)
Old 03-08-2011, 07:33 AM   PM User | #6
schuhmi2
New to the CF scene

 
Join Date: Mar 2011
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
schuhmi2 is an unknown quantity at this point
Thanks @Old Pedant, It looks like when combining it all into one statement, I somehow managed to make a minor error (stupid brackets)

Thanks for the help

RESOLVED
schuhmi2 is offline   Reply With Quote
Reply

Bookmarks

Tags
error, perl, query

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:11 AM.


Advertisement
Log in to turn off these ads.