Home  >  Article  >  Backend Development  >  How can we create a MySQL table using PHP script?

How can we create a MySQL table using PHP script?

PHPz
PHPzforward
2023-08-28 14:33:061032browse

How can we create a MySQL table using PHP script?

We know that PHP provides a function called mysql_query to create MySQL tables. To illustrate this, we use the following example −

In this example, we create a table named 'Tutorials_tbl' using a PHP script

Example

<html>
   <head>
      <title>Creating MySQL Tables</title>
   </head>
   <body>
      <?php
         $dbhost = &#39;localhost:3036&#39;;
         $dbuser = &#39;root&#39;;
         $dbpass = &#39;rootpassword&#39;;
         $conn = mysql_connect($dbhost, $dbuser, $dbpass);
         
         if(! $conn ) {
            die(&#39;Could not connect: &#39; . mysql_error());
         }
         echo &#39;Connected successfully<br />&#39;;
         $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( &#39;TUTORIALS&#39; );
         $retval = mysql_query( $sql, $conn );
         
         if(! $retval ) {
            die(&#39;Could not create table: &#39; . mysql_error());
         }
         echo "Table created successfully</p><p>";
         mysql_close($conn);
      ?>
   </body>
</html>

The above is the detailed content of How can we create a MySQL table using PHP script?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete