Jump to content

Help with PDO Create Table for mysql


markschum
Go to solution Solved by markschum,

Recommended Posts

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';
}
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.