markschum Posted November 3, 2013 Share Posted November 3, 2013 Hello all, I created a database for a website using PHPAdmin and I now am trying to make some php files to create the database and tables as a backup to allow anyone to recreate the site. I have the create database code working. The Create table using pdo fails with a message saying I have an sql error. PDOException: SQLSTATE[42000]: Syntax error or access violation: 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 ')' at line 18 in C:\wamp\www\create_table.php on line 33 This is my code , cobbled together from various examples. I dont see the error and have tried combinations of quotes around the table name and the field names. I had also removed the NOT NULL from the rn field. None of it worked. There was a charset and collation line at the end that I have removed. The auto increment field was added to make deletions easier by avoiding using every field in the delete where statement. Any comments appreciated. Is there a good tutorial on using pdo, I find the php manual very difficult to follow for determining what I need to do overall. <?php // create database table // database trial table new $dns = 'mysql:host=localhost;dbname=trialdb'; $user = 'root'; $pass = ''; $opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); $db = new PDO($dns, $user, $pass, $opt); $sql = "CREATE TABLE IF NOT EXISTS new ( rn int(6) NOT NULL AUTO_INCREMENT PRIMARY KEY, type varchar(10) NOT NULL, material varchar(10) NOT NULL, ring varchar(10) NOT NULL, finish varchar(10) NOT NULL, font varchar(20) NOT NULL, text varchar(20) NOT NULL, name varchar(20) NOT NULL, address varchar(40) NOT NULL, city varchar(20) NOT NULL, zipcode varchar(10) NOT NULL, email varchar(40) NOT NULL, style varchar(10) NOT NULL, orderdate varchar(10) NOT NULL, ip varchar(20) NOT NULL, )" ; $sq = $db->query($sql); if ($sq) { echo 'created'; } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted November 3, 2013 Share Posted November 3, 2013 try removing the final comma Quote Link to comment Share on other sites More sharing options...
Solution markschum Posted November 3, 2013 Author Solution Share Posted November 3, 2013 well that's embarrasing , I thought I had tried that , well, live and learn. it's working. thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.