可以使用 PHP 的 `mysqli` 扩展来执行 SQL 脚本。具体步骤如下:
- 连接到数据库;
- 打开 SQL 脚本文件并读取其中的 SQL 语句;
- 逐条执行 SQL 语句;
- 关闭 SQL 脚本文件;
- 关闭数据库连接。
以下是通过 mysqli 执行 SQL 脚本的示例代码:
<?php
$servername = "localhost";
$username = "root";
$password = "a56";
$dbname = "msldbalicrm";
// 连接数据库
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查数据库连接是否成功
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 打开 SQL 脚本文件,读取其中 SQL 语句
$sql_file = fopen("mysql.sql", "r");
$sql = fread($sql_file, filesize("mysql.sql"));
fclose($sql_file);
// 执行 SQL 语句
if (mysqli_multi_query($conn, $sql)) {
// 输出执行结果
do {
if ($result = mysqli_store_result($conn)) {
mysqli_free_result($result);
}
} while (mysqli_next_result($conn));
echo "SQL script executed successfully";
} else {
echo "Error executing SQL script: " . mysqli_error($conn);
}
// 关闭数据库连接
mysqli_close($conn);
?>
在上面的代码中,我们通过 `fopen()` 函数打开 SQL 脚本文件,使用 `fread()` 函数读取其中 SQL 语句,最后使用 `fclose()` 函数关闭 SQL 脚本文件。然后使用 `mysqli_multi_query()` 函数执行 SQL 语句,如果执行成功,使用 `mysqli_store_result()` 函数继续处理结果集。
需要注意的是,如果 SQL 脚本文件中包含多条 SQL 语句,则可能会因为语句之间的冲突而导致执行失败。在这种情况下,可以将 SQL 语句按照顺序分开执行,以避免执行冲突。
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for cms_ad
-- ----------------------------
DROP TABLE IF EXISTS `cms_ad`;
CREATE TABLE `cms_ad` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT '',
`pic` varchar(255) DEFAULT '',
`url` varchar(255) DEFAULT '',
`ordnum` int(10) DEFAULT '0',
`isshow` smallint(1) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of cms_ad
-- ----------------------------
-- ----------------------------
-- Table structure for cms_admin
-- ----------------------------
DROP TABLE IF EXISTS `cms_admin`;
CREATE TABLE `cms_admin` (
`adminid` int(10) NOT NULL AUTO_INCREMENT,
`adminname` varchar(50) DEFAULT '',
`adminpass` varchar(50) DEFAULT '',
`penname` varchar(20) DEFAULT '',
`pid` int(10) DEFAULT '0',
`logintimes` int(10) DEFAULT '0',
`lastlogindate` bigint(10) DEFAULT '0',
`lastloginip` varchar(50) DEFAULT '',
`readonly` smallint(1) DEFAULT '0',
`islock` smallint(1) DEFAULT '0',
PRIMARY KEY (`adminid`),
KEY `adminname` (`adminname`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of cms_admin
-- ----------------------------
-- ----------------------------
-- Table structure for cms_admin_log
-- ----------------------------
DROP TABLE IF EXISTS `cms_admin_log`;
CREATE TABLE `cms_admin_log` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`title` varchar(50) DEFAULT '',
`url` varchar(255) DEFAULT '',
`msg` varchar(255) DEFAULT '',
`ip` varchar(50) DEFAULT '',
`createdate` bigint(10) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of cms_admin_log
-- ----------------------------
-- ----------------------------
-- Table structure for cms_admin_login_log
-- ----------------------------
DROP TABLE IF EXISTS `cms_admin_login_log`;
CREATE TABLE `cms_admin_login_log` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`loginname` varchar(50) DEFAULT '',
`loginip` varchar(50) DEFAULT '',
`logindate` bigint(10) DEFAULT '0',
`loginmsg` varchar(255) DEFAULT '',
`loginstate` smallint(1) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of cms_admin_login_log
-- ----------------------------
-- ----------------------------
-- Table structure for cms_admin_menu
-- ----------------------------
DROP TABLE IF EXISTS `cms_admin_menu`;
CREATE TABLE `cms_admin_menu` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`title` varchar(50) DEFAULT '',
`cname` varchar(50) DEFAULT '',
`aname` varchar(50) DEFAULT '',
`dname` varchar(255) DEFAULT '',
`icon` varchar(50) DEFAULT '',
`followid` int(10) DEFAULT '0',
`ordnum` int(10) DEFAULT '0',
`islock` smallint(1) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Records of cms_admin_menu
-- ----------------------------
INSERT INTO `cms_admin_menu` VALUES ('1', '网站', '', '', '', 'ui-icon-setting', '0', '1', '1');
INSERT INTO `cms_admin_menu` VALUES ('2', '商品', '', '', '', 'ui-icon-shopping', '0', '3', '1');
INSERT INTO `cms_admin_menu` VALUES ('3', '订单', '', '', '', 'ui-icon-filter', '0', '5', '1');
INSERT INTO `cms_admin_menu` VALUES ('4', '报表', '', '', '', 'ui-icon-barchart', '0', '7', '1');
INSERT INTO `cms_admin_menu` VALUES ('5', '应用', '', '', '', 'ui-icon-appstore', '0', '9', '1');
INSERT INTO `cms_admin_menu` VALUES ('6', '系统', '', '', '', 'ui-icon-windows', '0', '11', '1');
INSERT INTO `cms_admin_menu` VALUES ('7', '网站设置', 'config', 'index', '', '', '1', '1', '1');
INSERT INTO `cms_admin_menu` VALUES ('8', '接口设置', 'configapi', 'index', '', '', '1', '3', '1');
INSERT INTO `cms_admin_menu` VALUES ('9', '部门管理', 'adminpart', 'index', '', '', '1', '7', '1');
INSERT INTO `cms_admin_menu` VALUES ('10', '后台用户', 'admin', 'index', '', '', '1', '9', '1');
INSERT INTO `cms_admin_menu` VALUES ('11', '商品分类', 'goodsclass', 'index', '', '', '2', '1', '1');
INSERT INTO `cms_admin_menu` VALUES ('12', '商品管理', 'goods', 'index', '', '', '2', '3', '1');
INSERT INTO `cms_admin_menu` VALUES ('13', '回收站', 'goodsrecycle', 'index', '', '', '2', '5', '1');
INSERT INTO `cms_admin_menu` VALUES ('14', '订单管理', 'order', 'index', '', '', '3', '1', '1');
INSERT INTO `cms_admin_menu` VALUES ('15', '回收站', 'orderrecycle', 'index', '', '', '3', '3', '1');
INSERT INTO `cms_admin_menu` VALUES ('16', '收入统计', 'count', 'index', '', '', '4', '1', '1');
INSERT INTO `cms_admin_menu` VALUES ('17', '订单统计', 'countorder', 'index', '', '', '4', '3', '1');
INSERT INTO `cms_admin_menu` VALUES ('18', '渠道统计', 'countfrom', 'index', '', '', '4', '5', '1');
INSERT INTO `cms_admin_menu` VALUES ('19', '模板管理', 'theme', 'index', '', '', '5', '1', '1');
INSERT INTO `cms_admin_menu` VALUES ('20', '插件管理', 'plug', 'index', '', '', '5', '3', '1');
INSERT INTO `cms_admin_menu` VALUES ('21', '设置分组', 'configgroup', 'index', '', '', '6', '1', '1');
INSERT INTO `cms_admin_menu` VALUES ('22', '后台菜单', 'adminmenu', 'index', '', '', '6', '3', '1');
INSERT INTO `cms_admin_menu` VALUES ('23', '管理日志', 'log', 'index', '', '', '6', '5', '1');
INSERT INTO `cms_admin_menu` VALUES ('24', '错误日志', 'logerror', 'index', '', '', '6', '7', '1');
INSERT INTO `cms_admin_menu` VALUES ('25', '缓存管理', 'cache', 'index', '', '', '6', '9', '1');
INSERT INTO `cms_admin_menu` VALUES ('26', '轮播图片', 'ad', 'index', '', '', '1', '5', '1');