如何在 PHP 中使用 shell_script 创建数据库后创建 table?

How to create table after create database using shell_script in PHP?

解决了在 php 中使用 shell_script 创建数据库的问题后,请参阅此 post link,我正在尝试创建 tables在同一个 shell_exec 命令中。

我有这些行:

$sql="CREATE DATABASE IF NOT EXISTS dbtest; use dbtest; 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
) ";

shell_exec("mysql -uroot -pmypasswrd  -e  \"$sql\" ");

这段代码只创建数据库,不创建table。我的问题是如何使用 shell_exec() 完成这项任务?是否可以使用 shell_exec() 运行 来自 mysql 转储的 sql 满足?

This code only creates the database, but not creates the table. My question is how accomplish this task using shell_exec()?

我能为您的问题找到的唯一解决方案是将整个 SQL 查询写在一行中(不要将其分成几行)。它会影响你的代码的可读性,但是你的 table 会被创建。