简体   繁体   English

PHP / MYSQL:在WAMP服务器中未创建表

[英]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 . 当我在我的wamp服务器中运行此php文件时,它会连接到数据库,选择数据库而不创建表。 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代码:

<?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), 删除PASSWORD CHAR(15),之后的逗号PASSWORD CHAR(15),

in the event of an error, you can always retrieve errors using mysql_error() . 如果发生错误,您始终可以使用mysql_error()检索错误。

however. 然而。 you should not be using the old mysql functions. 您不应该使用旧的mysql函数。 much better and more secure options exist in the PDO and MySQLi extensions. PDOMySQLi扩展中存在更好,更安全的选项。

Your $sql: 您的$ 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) 您应该在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. 确保您尝试创建具有该权限的表的MySQL用户。 (eg the user has been granted the 'CREATE' privileges for that database.) (例如,已为用户授予该数据库的“创建”权限。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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