要在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();
?>
请注意,这只是一个基本示例。具体的代码可能会根据你的数据库和表结构有所不同。你可以根据需要修改这些代码。