• <i id='m6WRH'><tr id='m6WRH'><dt id='m6WRH'><q id='m6WRH'><span id='m6WRH'><b id='m6WRH'><form id='m6WRH'><ins id='m6WRH'></ins><ul id='m6WRH'></ul><sub id='m6WRH'></sub></form><legend id='m6WRH'></legend><bdo id='m6WRH'><pre id='m6WRH'><center id='m6WRH'></center></pre></bdo></b><th id='m6WRH'></th></span></q></dt></tr></i><div id='m6WRH'><tfoot id='m6WRH'></tfoot><dl id='m6WRH'><fieldset id='m6WRH'></fieldset></dl></div>
    1. <tfoot id='m6WRH'></tfoot>
    2. <legend id='m6WRH'><style id='m6WRH'><dir id='m6WRH'><q id='m6WRH'></q></dir></style></legend>
        <bdo id='m6WRH'></bdo><ul id='m6WRH'></ul>

      <small id='m6WRH'></small><noframes id='m6WRH'>

        使用 PDO 创建表

        时间:2023-10-05
        • <tfoot id='Jp9rP'></tfoot>
          • <bdo id='Jp9rP'></bdo><ul id='Jp9rP'></ul>
              <tbody id='Jp9rP'></tbody>
          • <small id='Jp9rP'></small><noframes id='Jp9rP'>

                <legend id='Jp9rP'><style id='Jp9rP'><dir id='Jp9rP'><q id='Jp9rP'></q></dir></style></legend>

                1. <i id='Jp9rP'><tr id='Jp9rP'><dt id='Jp9rP'><q id='Jp9rP'><span id='Jp9rP'><b id='Jp9rP'><form id='Jp9rP'><ins id='Jp9rP'></ins><ul id='Jp9rP'></ul><sub id='Jp9rP'></sub></form><legend id='Jp9rP'></legend><bdo id='Jp9rP'><pre id='Jp9rP'><center id='Jp9rP'></center></pre></bdo></b><th id='Jp9rP'></th></span></q></dt></tr></i><div id='Jp9rP'><tfoot id='Jp9rP'></tfoot><dl id='Jp9rP'><fieldset id='Jp9rP'></fieldset></dl></div>
                  本文介绍了使用 PDO 创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我对 php 和这个论坛很陌生,所以请原谅任何错误或错位的问题.在我提供的代码中,我只是想在数据库mydb"中创建一个表.我测试了与数据库的连接(它有效).这只是我遇到问题的创建表.任何建议或批评将不胜感激.谢谢

                  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 />";
                  }
                  ?>
                  

                  推荐答案

                  由于创建表时没有行受到影响 $createTable 返回 0 参见 手册

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

                  PDO::exec() 返回修改或删除的行数通过您发出的 SQL 语句.如果没有行受到影响,PDO::exec()返回 0.

                  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.

                  当您创建表时,如果您的列名是硬编码的(如下面的代码所示),您将不会受到 SQL 注入的影响.我已经离开 $table = "tcompany"; 因为你想打印创建的表(我自己会忽略它)

                  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)

                  我添加了错误处理 将显示 try 块中的任何错误.

                  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
                  }
                  

                  注意回答评论使用

                  CREATE TABLE IF NOT EXISTS
                  

                  这篇关于使用 PDO 创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PHP 7.2.2 + mysql 8.0 PDO 给出:客户端未知的身份验证 下一篇:用 SQLite 计算大圆距离

                  相关文章

                  最新文章

                  <tfoot id='l3AML'></tfoot>
                  • <bdo id='l3AML'></bdo><ul id='l3AML'></ul>

                    <small id='l3AML'></small><noframes id='l3AML'>

                    <legend id='l3AML'><style id='l3AML'><dir id='l3AML'><q id='l3AML'></q></dir></style></legend>
                    1. <i id='l3AML'><tr id='l3AML'><dt id='l3AML'><q id='l3AML'><span id='l3AML'><b id='l3AML'><form id='l3AML'><ins id='l3AML'></ins><ul id='l3AML'></ul><sub id='l3AML'></sub></form><legend id='l3AML'></legend><bdo id='l3AML'><pre id='l3AML'><center id='l3AML'></center></pre></bdo></b><th id='l3AML'></th></span></q></dt></tr></i><div id='l3AML'><tfoot id='l3AML'></tfoot><dl id='l3AML'><fieldset id='l3AML'></fieldset></dl></div>