Như chúng ta biết rằng PHP cung cấp cho chúng ta hàm có tên mysql_query () để tạo một bảng MySQL. Tương tự, chúng ta có thể sử dụng mysql_query () hàm tạo bảng tạm thời MySQL. Để minh họa điều này, chúng tôi đang sử dụng ví dụ sau -
Ví dụ
Trong ví dụ này, chúng tôi đang tạo một bảng tạm thời có tên ‘ SalesSummary 'Với sự trợ giúp của tập lệnh PHP trong ví dụ sau -
<html>
<head>
<title>Creating MySQL Temporary Tables</title>
</head>
<body>
<?php
$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 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( 'TUTORIALS' );
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not create table: ' . mysql_error());
}
echo "Table created successfully\n";
mysql_close($conn);
?>
</body>
</html>