php - 使用 CodeIgniter 从 .sql 文件执行 SQL

标签 php mysql sql codeigniter

我有一个 SQL 脚本来生成数据库,我希望当我按下按钮时注册它调用 SQL 脚本,但在传递到 SQL 脚本有关如何调用数据库的数据之前。

有些像这样:

make_db($name_db);

最佳答案

$sql = file_get_contents("update-001.sql");

/*
Assuming you have an SQL file (or string) you want to run as part of the migration, which has a number of statements...
CI migration only allows you to run one statement at a time. If the SQL is generated, it's annoying to split it up and create separate statements.
This small script splits the statements allowing you to run them all in one go.
*/

$sqls = explode(';', $sql);
array_pop($sqls);

foreach($sqls as $statement){
    $statment = $statement . ";";
    $this->db->query($statement);   
}

原帖在这里:https://gist.github.com/Relequestual/4088557

关于php - 使用 CodeIgniter 从 .sql 文件执行 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25147099/

相关文章:

php - mysql 触发器与 php 脚本

php - mysql连接失败

javascript - 使用 javascript 获取值数据并删除行表

mysql - phpmyadmin 限制特定数据库的权限

php - 来自 2 个 mysql 表的动态下拉列表

php - 上传的图片不保存在数据库中

javascript - 在 Node JS for 循环中递增 SQL 结果[0]

WordPress 主题中 PHP 变量到 js 变量

mysql - 在 Sequelize 中急切加载 belongsToMany 关联的结果

php - 如何使用 PHP 和 SQL 更改按年份分组的导航的月份顺序