Skip to main content

PHP Creating MySQL Tables

Screenshot
Body

PHP Creating MySQL Tables

Before, I created a tutorial for PHP MySQL Database. For the follow-up tutorial, we are going to learn on How To Create MySQL Table In The Database. In the database, it will consist one or more tables. So, let’s start to create MySQL Table.

Note: All database table has a unique name.

Using MySQLi and PDO To Create MySQL Table.

We are going to use CREATE TABLE statement to make a table in MySQL.

  1. -- --------------------------------------------------------</p>
  2. <p>--<br />
  3. -- Table structure for table `tbl_registration`<br />
  4. --</p>
  5. <p>CREATE TABLE `tbl_registration` (<br />
  6. `tbl_registration_id` INT(11) NOT NULL,<br />
  7. `first_name` VARCHAR(100) NOT NULL,<br />
  8. `middle_name` VARCHAR(100) NOT NULL,<br />
  9. `last_name` VARCHAR(100) NOT NULL,<br />
  10. `email` VARCHAR(100) NOT NULL,<br />
  11. `contact_number` VARCHAR(100) NOT NULL<br />
  12. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

So, we are going to create our table, name as “tbl_registration” and contain with “tbl_registration_id”, “first_name”, “middle_name”, “last_name”, “email”, “contact_number”.

Note: In each database table has an individual primary key. In the example table above, our primary key is “tbl_registration_id”. And it's value is unique.

Creating Database Table Using MySQLi (Object-Oriented)

  1. <?php<br />
  2. $servername = "localhost";<br />
  3. $username = "username";<br />
  4. $password = "password";<br />
  5. $dbname = "add_query_pdo";</p>
  6. <p>// Create connection<br />
  7. $conn = new mysqli($servername, $username, $password, $dbname);<br />
  8. // Check connection<br />
  9. if ($conn->connect_error) {<br />
  10. die("Connection Error. Check your syntax." . $conn->connect_error);<br />
  11. } </p>
  12. <p>// sql to create table<br />
  13. $sql = "CREATE TABLE `tbl_registration` (<br />
  14. `tbl_registration_id` int(11) NOT NULL,<br />
  15. `first_name` varchar(100) NOT NULL,<br />
  16. `middle_name` varchar(100) NOT NULL,<br />
  17. `last_name` varchar(100) NOT NULL,<br />
  18. `email` varchar(100) NOT NULL,<br />
  19. `contact_number` varchar(100) NOT NULL<br />
  20. )";</p>
  21. <p>if ($conn->query($sql) === TRUE) {<br />
  22. echo "Your Table tbl_registration Successfully Created!!!";<br />
  23. } else {<br />
  24. echo "Error creating table. Check the Syntax." . $conn->error;<br />
  25. }</p>
  26. <p>$conn->close();<br />
  27. ?>

Creating Database Table Using MySQLi (Procedural)

  1. <?php<br />
  2. $servername = "localhost";<br />
  3. $username = "username";<br />
  4. $password = "password";<br />
  5. $dbname = "add_query_pdo";</p>
  6. <p>// Create connection<br />
  7. $conn = mysqli_connect($servername, $username, $password, $dbname);<br />
  8. // Check connection<br />
  9. if (!$conn) {<br />
  10. die("Connection Error. Check your syntax." . mysqli_connect_error());<br />
  11. }</p>
  12. <p>// sql to create table<br />
  13. $sql = "CREATE TABLE `tbl_registration` (<br />
  14. `tbl_registration_id` int(11) NOT NULL,<br />
  15. `first_name` varchar(100) NOT NULL,<br />
  16. `middle_name` varchar(100) NOT NULL,<br />
  17. `last_name` varchar(100) NOT NULL,<br />
  18. `email` varchar(100) NOT NULL,<br />
  19. `contact_number` varchar(100) NOT NULL<br />
  20. )";</p>
  21. <p>if (mysqli_query($conn, $sql)) {<br />
  22. echo "Your Table tbl_registration Successfully Created!!!";<br />
  23. } else {<br />
  24. echo "Error creating table. Check the Syntax." . mysqli_error($conn);<br />
  25. }</p>
  26. <p>mysqli_close($conn);<br />
  27. ?>

Creating Database Table Using PDO (PHP Data Objects)

  1. <?php<br />
  2. $servername = "localhost";<br />
  3. $username = "username";<br />
  4. $password = "password";<br />
  5. $dbname = "add_query_pdo";</p>
  6. <p>try {<br />
  7. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);<br />
  8. // set the PDO error mode to exception<br />
  9. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);</p>
  10. <p> // sql to create table<br />
  11. $sql = "CREATE TABLE `tbl_registration` (<br />
  12. `tbl_registration_id` int(11) NOT NULL,<br />
  13. `first_name` varchar(100) NOT NULL,<br />
  14. `middle_name` varchar(100) NOT NULL,<br />
  15. `last_name` varchar(100) NOT NULL,<br />
  16. `email` varchar(100) NOT NULL,<br />
  17. `contact_number` varchar(100) NOT NULL<br />
  18. )";</p>
  19. <p> // use exec() because no results are returned<br />
  20. $conn->exec($sql);<br />
  21. echo "Your Table tbl_registration Successfully Created!!!";<br />
  22. }<br />
  23. catch(PDOException $e)<br />
  24. {<br />
  25. echo $sql . "<br>" . $e->getMessage();<br />
  26. }</p>
  27. <p>$conn = null;<br />
  28. ?>

Output:

Result

So, this is it. You have one table in your database. And you can create more tables in your database. For my next tutorial, we are going to learn on PHP Insert Data In MySQL.

Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment