要使用PHP創建數據庫和表,你需要使用MySQL數據庫管理系統并通過PHP的MySQLi或PDO擴展來與數據庫進行交互。
以下是創建數據庫和表的步驟:
$servername = "localhost";
$username = "username";
$password = "password";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->select_db("myDB");
$sql = "CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
echo "Table created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
以上代碼示例演示了如何使用PHP連接到MySQL數據庫服務器、創建數據庫、選擇數據庫、創建表,并關閉數據庫連接。你可以根據自己的需求和表結構修改相應的SQL語句。