0

when I run this php file in my wamp server it connects to database, selects database but not create table . the output is this:

connected to database succussfully.
connected to database store_db succussfully.
table users was not created.

what is the problem???

php code:

<?php
$sql='CREATE TABLE users
(
PID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(PID),
NAME CHAR(20),
PASSWORD CHAR(15),
)';

mysql_connect("localhost","root","") or die('could not connect to database.');
echo 'connected to database succussfully.<br>';

mysql_select_db('store_db') or die('database store_db not found.');
echo 'connected to database store_db succussfully.<br>';

mysql_query($sql) or die('table users was not created');
echo 'table users was created in database store_db succussfully.<br>';

?>

4 Answers 4

3
<?php
$sql='CREATE TABLE users
(
PID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(PID),
NAME CHAR(20),
PASSWORD CHAR(15),
)';

remove the comma after PASSWORD CHAR(15),

in the event of an error, you can always retrieve errors using mysql_error().

however. you should not be using the old mysql functions. much better and more secure options exist in the PDO and MySQLi extensions.

Sign up to request clarification or add additional context in comments.

Comments

1

Your $sql:

$sql='CREATE TABLE users
(
PID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(PID),
NAME CHAR(20),
PASSWORD CHAR(15),
)';

should be:

$sql='CREATE TABLE users
(
PID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(PID),
NAME CHAR(20),
PASSWORD CHAR(15)
)';

You should remove comma right after PASSWORD CHAR(15)

Comments

0

To get the proper error message always use below code so that you can figure out exact error in future.

mysql_query($sql) or die(mysql_error());

1 Comment

Instead of guessing just use code similar to above ,so that you may figure out exact error.
0

Make sure the MySQL user you are attempting to create the table with has permissions to do so. (e.g. the user has been granted the 'CREATE' privileges for that database.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.