简体   繁体   English

到目前为止,我正在尝试使用PHP在数据库上创建表,但是我设法使用PHP创建了数据库,但无法创建表/表

[英]I'm trying to create a table on my database using PHP so far i have managed to create a database using PHP but but fail to create table/tables

<?PHP

    try{
        $handler = new PDO($servername, $username, $password);
        $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "CREATE TABLE user_details ( 
            id INT NOT NULL AUTO_INCREMENT,
            email VARCHAR(30) NOT NULL,
            fullname VARCHAR(30) NOT NULL,
            username VARCHAR(30) NOT NULL,
            password VARCHAR(30)  NOT NULL,
            gender ENUM('male','female','other','')  NOT NULL , PRIMARY KEY (id)) 
            ENGINE = InnoDB
            )";
        echo "Sucessfullll";
        $handler->exc($sql);
    } catch (PDOException $msg){
        echo $msg->getMessage();
        die();
    }
?>

You have an extra unmatched parentheses ) 您有一个多余的括号( )

$sql = "CREATE TABLE user_details ( 
           id INT NOT NULL AUTO_INCREMENT,
           email VARCHAR(30) NOT NULL,
           fullname VARCHAR(30) NOT NULL,
           username VARCHAR(30) NOT NULL,
           password VARCHAR(30)  NOT NULL,
           gender ENUM('male','female','other','')  NOT NULL ,
           PRIMARY KEY (id)
      ) ENGINE = InnoDB )";

Should be 应该

$sql = "CREATE TABLE user_details ( 
           id INT NOT NULL AUTO_INCREMENT,
           email VARCHAR(30) NOT NULL,
           fullname VARCHAR(30) NOT NULL,
           username VARCHAR(30) NOT NULL,
           password VARCHAR(30)  NOT NULL,
           gender ENUM('male','female','other','')  NOT NULL ,
           PRIMARY KEY (id)
      ) ENGINE = InnoDB";

You also might need the length for the INT, I forget. 我忘记了,您可能还需要INT的长度。

INDEXES 指标

I always do my id's like this id INT(10) UNSINGED NOT NULL AUTO_INCREMENT because well no one like having a negative ID. 我总是像这样的id INT(10) UNSINGED NOT NULL AUTO_INCREMENT做我的id INT(10) UNSINGED NOT NULL AUTO_INCREMENT因为没有人喜欢拥有负ID。 And I would make UNIQUE(username) And UNIQUE(email) and maybe an INDEX(fullname) . 然后我将使用UNIQUE(username)UNIQUE(email)以及INDEX(fullname)

Indexes depend on what you plan on searching, the more indexes you have the long it takes to insert and update records. 索引取决于您计划搜索的内容,插入和更新记录所需的时间越长,索引越多。 But for your average website it's pretty unnoticeable. 但是对于您的普通网站来说,这是非常不明显的。

If you plan on inserting 10's of thousands of rows at a time, you would want to be more careful with indexes, because for each index it takes a bit extra time to compile them when modifying the data. 如果您打算一次插入十万行,那么您将要对索引更加小心,因为对于每个索引,在修改数据时需要花费一些额外的时间来编译它们。

NAMING CONVENTIONS 命名惯例

Your field names look good, no plurals no casing. 您的字段名称看起来不错,没有复数形式也没有大小写。 The only thing I would change here is name the table just users , I usually pluralize tables, and use _ as a way to mark a junction table, or a table that ties 2 tables together with a Many To Many relationship. 我在这里唯一要更改的是将表命名为users ,我通常将表复数,并使用_作为标记联结表或将2个表与多对多关系联系在一起的表的方法。 So for example I would have users and addresses and then a table that joined them users_addresses . 因此,例如,我将拥有usersaddresses ,然后有一个将它们连接到users_addresses的表。

If it's a table with a One to many, I will do a non-plural for the one and a plural for the many. 如果它是一个具有一对多的表,那么我将对一个进行非复数处理,对多个进行复数处理。 So if each user could have many uploads. 因此,如果每个用户可以上传许多文件。 One to Many, I would name it like user_uploads . 一对多,我将其命名为user_uploads That way I can see what kind of relationship it is without opening up PHPmyAdmin or trying to remember how I set it up. 这样,无需打开PHPmyAdmin或尝试记住我的设置方式,就可以了解它之间的关系。

I just wanted to mention that, because the name you have going by my naming conventions, it would be a table with details in it with a user_id foreign key. 我只想提及这一点,因为按照我的命名约定,您要使用的名称将是一个带有用户外键user_id的表。

I should say everyone does naming conventions a bit differently the main thing is to name things for a reason, and be consistent. 我应该说每个人对约定的命名方式有所不同,主要是出于某种原因命名事物并保持一致。

last thing 最后一件事

This is just my Opinion, but I find it better to connect this way: 这只是我的意见,但我发现以这种方式进行连接会更好:

$options = [
     PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
     PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
];

$handler = new PDO($servername, $username, $password, $options);

By sending it as options, it will throw an exception when you fail to connect. 通过将其作为选项发送,当您连接失败时,它将引发异常。 Otherwise you haven't set the mode until after the initial connection. 否则,直到初始连接后,您才需要设置模式。 And probably about 90% of the time I use associative arrays for my return, so you can set that too. 大概有90%的时间我使用关联数组作为返回值,因此您也可以进行设置。

cheers! 干杯!

I'm not sure what value you have stored in $servername variable. 我不确定您在$servername变量中存储了什么值。 If you are storing just the IP address or hostname, then it's wrong. 如果仅存储IP地址或主机名,那是错误的。

The first argument to the PDO constructor is a Data Source Name ( dsn ). PDO构造函数的第一个参数是数据源名称( dsn )。 An example of a MySQL DSN is: MySQL DSN的一个示例是:

$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$handler = new PDO($dsn, $username, $password);

See http://php.net/manual/en/pdo.construct.php 参见http://php.net/manual/en/pdo.construct.php

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

相关问题 我正在尝试使用参数创建可重用的php函数以从数据库中的表中选择数据 - I'm trying to create a reusable php function with parameters to select data from tables in my database 使用php从数据库创建和更新表 - Create and Update a table from database using php 我有某种php代码,在此我想将连接变量($ conn)发送到另一个页面中,以便我可以动态创建数据库表 - I have sort of php code, in this i want to send connection variable ($conn) into another page so that i can create table for database dynamically PHP表:我正在尝试为网站中的每个mysql表行创建一个新行 - PHP Table: I'm trying to create new row for every mysql table rows in my website 尝试使用PHP从数据库读取和创建表 - Trying to Read and Create Tables From Database using Php 我应该使用数据库还是PHP创建哈希 - Should I create hash using database or php 我可以在 PHP 中使用 PDO 创建数据库吗? - Can I create a database using PDO in PHP? 我需要创建一个php表单来使用从上一次查询中获得的ID更新数据库表 - I need to create a php form to update a database table using the IDs I got from a previous query 我应该使用HTML或PHP创建表吗? - Should I create a table using HTML or PHP? 如何使用php使用备份文件创建包含表和信息的数据库? - How do I use php to create a database with table and info using a backup file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM