在PHP中,可以使用以下方法来创建数据表:
$conn = new mysqli($servername, $username, $password, $dbname);
$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 users created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$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
)";
$conn->exec($sql);
echo "Table users created successfully";
} catch(PDOException $e) {
echo "Error creating table: " . $e->getMessage();
}
$conn = null;
use Doctrine\ORM\Tools\SchemaTool;
// Get the EntityManager
$entityManager = GetEntityManager();
// Get the Metadata
$metadata = $entityManager->getClassMetadata('User');
// Create the SchemaTool
$schemaTool = new SchemaTool($entityManager);
// Create the Table
$schemaTool->createSchema([$metadata]);
这些方法可以根据需要选择适合的方式来创建数据表。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: PHP扩展开发怎样提升扩展性