简体   繁体   English

使用 PDO 创建表

[英]Using PDO to CREATE TABLE

I am very new to php and this forum, so please excuse any errors or misplaced questions.我对php和这个论坛很陌生,所以请原谅任何错误或放错地方的问题。 In the code i provided, I am just looking to CREATE a Table in the DB "mydb".在我提供的代码中,我只是想在数据库“mydb”中创建一个表。 I tested the connection to the DB(It works).我测试了与数据库的连接(它有效)。 It is just the creating the table i am having issues with.这只是创建我遇到问题的表。 Any advice or criticisms would be appreciated.任何建议或批评将不胜感激。 Thx谢谢

<?php
/*
*
* File:         PDOcreateTabletcompany.php
* By:          Jay
* Date:       24-10-13
*
*  This script createsTableintoDB
*
*====================================
*
*/
try {
    $db = new PDO("mysql:dbname=mydb;host=localhost", "root", "" );
} catch(PDOException $e) {
    echo $e->getMessage();
}
$table= "tcompany";
$columns = "ID INT( 11 ) AUTO_INCREMENT PRIMARY KEY, Prename VARCHAR( 50 ) NOT NULL, Name VARCHAR( 250 ) NOT NULL,
 StreetA VARCHAR( 150 ) NOT NULL, StreetB VARCHAR( 150 ) NOT NULL, StreetC VARCHAR( 150 ) NOT NULL, 
 County VARCHAR( 100 ) NOT NULL, Postcode VARCHAR( 50 ) NOT NULL, Country VARCHAR( 50 ) NOT NULL " ;


$createTable = $db->exec("CREATE TABLE IF NOT EXISTS mydb.$table ($columns)");

if ($createTable) 
{
    echo "Table $table - Created!<br /><br />";
}
else { echo "Table $table not successfully created! <br /><br />";
}
?>

As no rows are affected when creating table $createTable returns 0 see manual由于创建表时没有行受到影响 $createTable 返回 0 请参阅手册

PDO::exec() returns the number of rows that were modified or deleted by the SQL statement you issued. PDO::exec() 返回由您发出的 SQL 语句修改或删除的行数。 If no rows were affected,PDO::exec() returns 0.如果没有行受到影响,PDO::exec() 返回 0。

As you are CREATING a table you will be free from SQL injection if your column names are hard coded( as in the code below).当您创建一个表时,如果您的列名是硬编码的(如下面的代码),您将不会受到 SQL 注入的影响。 I have left $table = "tcompany";我离开$table = "tcompany"; as you want to print table created( I would leave it out myself)当您想打印创建的表时(我自己会忽略它)

I have added error-handling which will show any errors in try block.我添加了错误处理,它将显示try块中的任何错误。

$table = "tcompany";
try {
     $db = new PDO("mysql:dbname=mydb;host=localhost", "root", "" );
     $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );//Error Handling
     $sql ="CREATE table $table(
     ID INT( 11 ) AUTO_INCREMENT PRIMARY KEY,
     Prename VARCHAR( 50 ) NOT NULL, 
     Name VARCHAR( 250 ) NOT NULL,
     StreetA VARCHAR( 150 ) NOT NULL, 
     StreetB VARCHAR( 150 ) NOT NULL, 
     StreetC VARCHAR( 150 ) NOT NULL, 
     County VARCHAR( 100 ) NOT NULL,
     Postcode VARCHAR( 50 ) NOT NULL,
     Country VARCHAR( 50 ) NOT NULL);" ;
     $db->exec($sql);
     print("Created $table Table.\n");

} catch(PDOException $e) {
    echo $e->getMessage();//Remove or change message in production code
}

NOTE in answer to comment use注意回答评论使用

CREATE TABLE IF NOT EXISTS

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

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