Thread: Syntax Error
View Single Post
  #1 (permalink)  
Old 05-14-08, 12:27 PM
Nikas Nikas is offline
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.
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?
Reply With Quote