doupo5178 2013-10-24 21:30
浏览 71
已采纳

使用PDO创建表

I am very new to php and this forum, so please excuse any errors or misplaced questions. In the code i provided, I am just looking to CREATE a Table in the DB "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 />";
}
?>
  • 写回答

1条回答 默认 最新

  • douji8033 2013-10-25 18:14
    关注

    As no rows are affected when creating table $createTable returns 0 see manual

    PDO::exec() returns the number of rows that were modified or deleted by the SQL statement you issued. If no rows were affected,PDO::exec() returns 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). I have left $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.

    $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.
    ");
    
    } catch(PDOException $e) {
        echo $e->getMessage();//Remove or change message in production code
    }
    

    NOTE in answer to comment use

    CREATE TABLE IF NOT EXISTS
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加