要在PHP中創建數據庫和表,你可以使用SQL查詢語句來實現。以下是一個基本的例子:
創建數據庫:
<?php
$servername = "localhost";
$username = "root";
$password = "";
// 創建連接
$conn = new mysqli($servername, $username, $password);
// 檢測連接
if ($conn->connect_error) {
die("連接失敗: " . $conn->connect_error);
}
// 創建數據庫
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === true) {
echo "數據庫創建成功";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
創建表:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// 創建連接
$conn = new mysqli($servername, $username, $password, $dbname);
// 檢測連接
if ($conn->connect_error) {
die("連接失敗: " . $conn->connect_error);
}
// 創建表
$sql = "CREATE TABLE myTable (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";
if ($conn->query($sql) === true) {
echo "表創建成功";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
請注意,這只是一個基本示例。具體的代碼可能會根據你的數據庫和表結構有所不同。你可以根據需要修改這些代碼。