How to Create Tables in Database of MySQL Using PHP

How to Create Tables in MySQL Database

In order to manipulate the database, you need tables inside the databases that actually holds the data. Without any tables, you cannot store any data in your MySQL database.

To create a table for MySQL database, you have to first create a SQL create statement and pass the statement to PHP function. There are two alternatives to PHP function you can use to create tables.

  1. mysqli_query()
  2. PDO::__query()

MySQLi Procedural
The process is the procedural way designing code and executing the query. You can use the PHP procedural method to create a MySQL database for your project.

A simple example is given below to use in your project as well as test it to your local Xamp or Wamp server. The credentials like hostname, username, and password are given below taken for the localhost. But, If you using another server like any hosting server, you can always change the below-given credentials.

MySQLi Object-oriented
While Developing an application, Using Object-oriented in your coding is the best practice to design a code. You can create the database connection, create your query and execute them by using PHP object-oriented programming.

The process is not different from procedural, only the way of coding is different. Below is the simple object-oriented programming you can use in your project code to create a table for your MySQL database using PHP.