使用 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
推荐答案
由于创建表时没有行受到影响 $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.
注意回答评论使用
这篇关于使用 PDO 创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!