Skip to content
Advertisement

Using PDO to CREATE TABLE

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

JavaScript

Advertisement

Answer

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.

JavaScript

NOTE in answer to comment use

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement