简体   繁体   中英

PHP/MYSQL: table was not created in wamp server

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>';

?>
<?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.

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)

要获取正确的错误消息,请始终使用以下代码,以便将来找出确切的错误。

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM