以下实例展示了如何在 PHP 中创建表:
实例 (MySQLi - 面向对象)
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
conn=newmysqli(conn = new mysqli(servername, username,username, password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 使用 sql 创建数据表
$sql = "CREATE TABLE MyGuests (
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(conn->query(sql) === TRUE) {
echo "Table MyGuests created successfully";
} else {
echo "创建数据表错误: " . $conn->error;
}
$conn->close();
?>