Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    110
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Simple MYSQL Syntax error

    I'm getting this error when i used the code below in my server. it works perfectly in my localhost. Could you please rectify the codes?

    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 'Book ( ID INT(10) NOT NULL auto_increment, Timestamp VARCHAR(14) NOT NULL, Name ' at line 1

    This is the MYSQL Code:
    Code:
    if($create == 'yes') {
    				$sql = "CREATE TABLE $tbl_name ( " .
    					   "$fld_id INT(10) NOT NULL auto_increment, " .
    					   "$fld_timestamp VARCHAR(14) NOT NULL, " .
    					   "$fld_name VARCHAR(50), " .
    					   "$fld_email VARCHAR(75), " .
    					   "$fld_subject VARCHAR(50) NOT NULL, " .
    					   "$fld_text TEXT NOT NULL, " .
    					   "PRIMARY KEY ($fld_id))";
    				if(!mysql_query($sql)) echo '<div class="cssError">' . mysql_error() . '</div>';
    				else $table_exists = true;
    			}

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Please post the output of
    PHP Code:
    echo $sql "CREATE TABLE $tbl_name ( " .
                           
    "$fld_id INT(10) NOT NULL auto_increment, " .
                           
    "$fld_timestamp VARCHAR(14) NOT NULL, " .
                           
    "$fld_name VARCHAR(50), " .
                           
    "$fld_email VARCHAR(75), " .
                           
    "$fld_subject VARCHAR(50) NOT NULL, " .
                           
    "$fld_text TEXT NOT NULL, " .
                           
    "PRIMARY KEY ($fld_id))"
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    hackerzlab (09-15-2011)

  • #3
    Regular Coder
    Join Date
    Feb 2006
    Posts
    110
    Thanks
    23
    Thanked 0 Times in 0 Posts
    I get this error when i tried it from the phpmyadmin using the code given above in the SQL tab:
    Code:
    #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 'echo $sql = "CREATE TABLE $tbl_name ( " . "$fld_id INT(10' at line 1

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    I haven't asked you to put that PHP string in phpmyadmin. Just echo that string as shown above and post the result here so that we may be able to track the source of error.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Error is obvious:
    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 'Book ( ID INT(10) NOT NULL auto_increment, Timestamp VARCHAR(14) NOT NULL, Name ' at line 1
    TIMESTAMP is a reserved name in MySQL.

    If you are going to use code like this to generate tables (which I personally think is a really really bad idea, but that's a topic for another discussion), then you really need to "escape" EACH AND EVERY table and field name, just in case of conflict. In MySQL, you "escape" using `...` (backticks...usually on same key as the ~ tilde).

    So:
    Code:
    $sql = "CREATE TABLE `$tbl_name` ( " .
    	   "`$fld_id` INT(10) NOT NULL auto_increment, " .
    	   "`$fld_timestamp` VARCHAR(14) NOT NULL, " .
    	   "`$fld_name` VARCHAR(50), " .
    	   "`$fld_email` VARCHAR(75), " .
    	   "`$fld_subject` VARCHAR(50) NOT NULL, " .
    	   "`$fld_text` TEXT NOT NULL, " .
    	   "PRIMARY KEY (`$fld_id`))";
    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.

  • Users who have thanked Old Pedant for this post:

    hackerzlab (09-15-2011)

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    By the by, why in the [censored] would you *EVER* use a field named TIMESTAMP and give it a data type of VARCHAR??????

    The implication of $fld_timestamp, to me, is that the field *should* be of type TIMESTAMP.

    Code:
             "`$fld_timestamp` TIMESTAMP NOT NULL, " .
    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.

  • Users who have thanked Old Pedant for this post:

    hackerzlab (09-15-2011)

  • #7
    Regular Coder
    Join Date
    Feb 2006
    Posts
    110
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Thank You guys.. really appreciate the help. i've got it working

    its an open source (2005) and i didn't code it myself. You guys are the best. Thanx again.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Ahhh...the joys of open source. It's free...and it's often bad coding. So you have to take the bad with the good. Best of luck with it.
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •