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

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

WBOY
Release: 2023-08-28 22:14:02
forward
1533 people have browsed it

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

We know that PHP provides us with a function called mysql_query() to create MySQL tables. Similarly, we can use the mysql_query() function to create a MySQL temporary table. To illustrate this, we use the following example -

Example

In this example, we create a temporary table named 'SalesSummary' In the following example With the help of PHP script-

<html>
   <head>
      <title>Creating MySQL Temporary 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 TEMPORARY TABLE SalesSummary( ".
            "Product_Name VARCHAR(50) NOT NULL, ".
            "total_sales DECIMAL(12,2) NOT NULL DEFAULT 0.00, ".
            "avg_unit_price DECIMAL(7,2) NOT NULL DEFAULT 0.00, ".
            "total_units_sold INT UNSIGNED NOT NULL DEFAULT 0, ".); ";
         
         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>
Copy after login

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

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template