Current location: Hot Scripts Forums » Other Discussions » Database » Syntax Error

Syntax Error

Reply
  #1 (permalink)  
Old 05-14-08, 12:27 PM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 307
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.
PHP Code:
$fp fopen('schema/mysql.sql''r');
    
$schema fread($fpfilesize('schema/mysql.sql'));
    
fclose($fp);
    
    
$sqls explode("\n"$schema);
    foreach (
$sqls as $sql)
    {
        
$pos strpos($sql'phpdesk_');
        
$table substr($sql$pos);
        
$pos strpos($table'`');
        
$table substr($table0$pos);

        if(
$db->query($sql))
        {
            if ( 
preg_match("/CREATE TABLE (\S+) \(/"$sql ))
            {
                echo 
'Table Created Successfully: '.$table."<br />\n";
            }
        }
        else
        {
            echo 
'Cannot Create Table: '.$table."<br />\n";
        }
    } 
SQL Query
sql Code:
  1. DROP TABLE IF EXISTS `phpdesk_admin`
  2. CREATE TABLE `phpdesk_admin` (
  3.   `id` int(255) NOT NULL DEFAULT '0',
  4.   `name` varchar(255) NOT NULL DEFAULT '',
  5.   `pass` varchar(255) NOT NULL DEFAULT '',
  6.   `email` varchar(255) NOT NULL DEFAULT '',
  7.   `notify_pm` int(1) NOT NULL DEFAULT '1',
  8.   `notify_response` int(1) NOT NULL DEFAULT '1',
  9.   `notify_ticket` int(1) NOT NULL DEFAULT '1',
  10.   `tppage` int(255) NOT NULL DEFAULT '25',
  11.   `signature` mediumtext NOT NULL,
  12.   PRIMARY KEY  (`id`)
  13. )

It gives me this error.

Code:
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 '' at line 1Cannot Create Table: phpdesk_admin
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 '`id` int(255) NOT NULL default '0',' at line 1Cannot Create Table:
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 '`name` varchar(255) NOT NULL default '',' at line 1Cannot Create Table:
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 '`pass` varchar(255) NOT NULL default '',' at line 1Cannot Create Table:
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 '`email` varchar(255) NOT NULL default '',' at line 1Cannot Create Table:
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 '`notify_pm` int(1) NOT NULL default '1',' at line 1Cannot Create Table:
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 '`notify_response` int(1) NOT NULL default '1',' at line 1Cannot Create Table:
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 '`notify_ticket` int(1) NOT NULL default '1',' at line 1Cannot Create Table:
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 '`tppage` int(255) NOT NULL default '25',' at line 1Cannot Create Table:
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 '`signature` mediumtext NOT NULL,' at line 1Cannot Create Table:
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 'PRIMARY KEY (`id`)' at line 1Cannot Create Table: PRIMARY KEY (
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 ')' at line 1Cannot Create Table:
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:
  1. DROP TABLE IF EXISTS `phpdesk_admin`
  2. CREATE TABLE `phpdesk_admin` ( 
  3. `id` int(255) NOT NULL DEFAULT '0'
  4. `name` varchar(255) NOT NULL DEFAULT ''
  5. `pass` varchar(255) NOT NULL DEFAULT ''
  6. `email` varchar(255) NOT NULL DEFAULT ''
  7. `notify_pm` int(1) NOT NULL DEFAULT '1'
  8. `notify_response` int(1) NOT NULL DEFAULT '1'
  9. `notify_ticket` int(1) NOT NULL DEFAULT '1'
  10. `tppage` int(255) NOT NULL DEFAULT '25'
  11. `signature` mediumtext NOT NULL
  12. 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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-15-08, 04:32 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community VIP
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 2,724
Thanks: 0
Thanked 0 Times in 0 Posts
The reason for the error lies within this line:
PHP Code:
$sqls explode("\n"$schema); 
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:
  1. DROP TABLE IF EXISTS `phpdesk_admin`
next query:
SQL Code:
  1. CREATE TABLE `phpdesk_admin` (
next query:
SQL Code:
  1. `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:
SQL Code:
  1. source 'schema/mysql.sql
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

If you want to add me on any IM, pm me first
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-15-08, 11:07 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 307
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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 05-15-08, 11:46 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 307
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 05-15-08, 11:48 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 2,651
Thanks: 0
Thanked 21 Times in 21 Posts
Quote:
Originally Posted by Nikas View Post
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:
PHP Code:
$sqls explode("\n"$schema); 
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ and MSSQL tutorials? scott2500uk C/C++ 8 05-11-09 03:33 AM
I cannot find what i messed up. Parse error: syntax error, unexpected T_CONSTANT_ENCA bilicek.com PHP 3 01-31-08 05:54 PM
Parse error: syntax error p51d78th PHP 3 01-02-08 09:52 AM
MySQL Syntax error gigafare PHP 4 04-19-06 03:03 AM
You have an error in your SQL syntax. Help! SevEre PHP 7 09-08-04 06:05 AM


All times are GMT -5. The time now is 03:08 AM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.