How can we create a MySQL table by using PHP script?

As we know that PHP provides us the function named mysql_query to create a MySQL table. To illustrate this we are using the following example −

In this example, we are creating a table named ‘Tutorials_tbl’ with the help of PHP script in


      <title>Creating MySQL Tables</title>
         $dbhost = 'localhost:3036';
         $dbuser = 'root';
         $dbpass = 'rootpassword';
         $conn = mysql_connect($dbhost, $dbuser, $dbpass);
         if(! $conn ) {
            die('Could not connect: ' . mysql_error());
         echo 'Connected successfully<br />';
         $sql = "CREATE TABLE tutorials_tbl( ".
            "tutorial_id INT NOT NULL AUTO_INCREMENT, ".
            "tutorial_title VARCHAR(100) NOT NULL, ".
            "tutorial_author VARCHAR(40) NOT NULL, ".
            "submission_date DATE, ".
            "PRIMARY KEY ( tutorial_id )); ";
         mysql_select_db( 'TUTORIALS' );
         $retval = mysql_query( $sql, $conn );
         if(! $retval ) {
            die('Could not create table: ' . mysql_error());
         echo "Table created successfully
";          mysql_close($conn);       ?>    </body> </html>

Updated on: 22-Jun-2020


Kickstart Your Career

Get certified by completing the course

Get Started