
05-14-08, 12:27 PM
|
|
Coding Addict
|
|
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
|
|
|
Syntax Error
What's wrong with this ?
I have a set of SQL query inside .sql file. and php code to runs it.
PHP code that runs it.
SQL Query
sql Code:
DROP TABLE IF EXISTS `phpdesk_admin` CREATE TABLE `phpdesk_admin` ( `id` int(255) NOT NULL DEFAULT '0', `name` varchar(255) NOT NULL DEFAULT '', `pass` varchar(255) NOT NULL DEFAULT '', `email` varchar(255) NOT NULL DEFAULT '', `notify_pm` int(1) NOT NULL DEFAULT '1', `notify_response` int(1) NOT NULL DEFAULT '1', `notify_ticket` int(1) NOT NULL DEFAULT '1', `tppage` int(255) NOT NULL DEFAULT '25', `signature` mediumtext NOT NULL, PRIMARY KEY (`id`) )
It gives me this error.
I copied the query over to phpmyadmin and run it without any problem.
However, this query doesn't looks like there's problem.
sql Code:
DROP TABLE IF EXISTS `phpdesk_admin` CREATE TABLE `phpdesk_admin` ( `id` int(255) NOT NULL DEFAULT '0', `name` varchar(255) NOT NULL DEFAULT '', `pass` varchar(255) NOT NULL DEFAULT '', `email` varchar(255) NOT NULL DEFAULT '', `notify_pm` int(1) NOT NULL DEFAULT '1', `notify_response` int(1) NOT NULL DEFAULT '1', `notify_ticket` int(1) NOT NULL DEFAULT '1', `tppage` int(255) NOT NULL DEFAULT '25', `signature` mediumtext NOT NULL, PRIMARY KEY (`id`))
It's just one of the query in the sql file. The latter SQL query file works, but it's outdated. Thus, I used another updated SQL file, which is the former. And it doesn't allows me to create.
Any idea?
|

05-15-08, 04:32 AM
|
 |
Community Liaison
|
|
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
|
|
The reason for the error lies within this line:
This lines thells php to split the content of $schema on each "\n" php finds in the string.
The query you posted is definietely a multiline query, therefore it will be split over several array-items ( $sqls is an array). Basically you will be running queries like these:
SQL Code:
DROP TABLE IF EXISTS `phpdesk_admin`
next query:
SQL Code:
CREATE TABLE `phpdesk_admin` (
next query:
SQL Code:
`id` int(255) NOT NULL DEFAULT '0',
and so on.
As you can see, only the first query is correctly formatted, and correct syntax, and will therefor be run. The others will rresult in an error right away as it's completely invalid syntax.
Instead of splitting on "\n" (newlines), you should be splitting per query.
I'm not 100% sure, but I thought you could use mysqli_query to execute several queries at the same time. That way you could simply pass on the data in the .sql file, and run it on your server.
Or..., and this is probably the best way, you could run the following query:
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks
|

05-15-08, 11:07 AM
|
|
Coding Addict
|
|
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
|
|
So in another words, if I change the queries to single line. The query would works fine?
And thanks for your help!
|

05-15-08, 11:46 AM
|
|
Coding Addict
|
|
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Alright. I have remove all the empty space and put all to one line for a single query and it works fine now.
Thanks for your detailed explanation.
|

05-15-08, 11:48 AM
|
 |
Community Liaison
|
|
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
|
|
Quote:
Originally Posted by Nikas
So in another words, if I change the queries to single line. The query would works fine?
And thanks for your help!
|
What UrealEd is trying to say lies in the problem with this line of code: When you use "\n" you are splitting up the data in $schema by line brakes.
The problem with doing that, is the queries can span multiple lines, and you are breaking the queries up into individual lines at every line break.
And your getting the queries one line at a time, when you need to get the whole query. So you need to define away to separate the queries and explode on that value.
__________________
Jerry Broughton
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|