我正在为我正在开发的应用程序创建一个安装脚本,并且需要从 PHP 中动态创建数据库。我有它来创建数据库,但现在我需要加载几个 .sql 文件。我曾计划一次打开文件并 mysql_query 一行 - 直到我查看模式文件并意识到它们不仅仅是每行一个查询。
那么,我如何从 PHP 中加载一个 sql 文件(就像 phpMyAdmin 使用它的 import 命令所做的那样)?
原文由 Josh Smeaton 发布,翻译遵循 CC BY-SA 4.0 许可协议
我正在为我正在开发的应用程序创建一个安装脚本,并且需要从 PHP 中动态创建数据库。我有它来创建数据库,但现在我需要加载几个 .sql 文件。我曾计划一次打开文件并 mysql_query 一行 - 直到我查看模式文件并意识到它们不仅仅是每行一个查询。
那么,我如何从 PHP 中加载一个 sql 文件(就像 phpMyAdmin 使用它的 import 命令所做的那样)?
原文由 Josh Smeaton 发布,翻译遵循 CC BY-SA 4.0 许可协议
PHP 代码
我在 此 页面上找到的代码对我有用。此代码可以加载指定的 SQL 文件并将其导入 MySQL 数据库。我使用 phpMyAdmin 使用 导出到 SQL 的 WordPress 数据库 测试了这段代码,它运行良好。 (向下滚动查看评论版本)
<?php
$conn = new mysqli('localhost', 'root', '' , 'sql_auto_test_table');
$query = '';
$sqlScript = file('sqlFileName.sql');
foreach ($sqlScript as $line) {
$startWith = substr(trim($line), 0 ,2);
$endWith = substr(trim($line), -1 ,1);
if (empty($line) || $startWith == '--' || $startWith == '/*' || $startWith == '//') {
continue;
}
$query = $query . $line . "/*<br>*/";
if ($endWith == ';') {
mysqli_query($conn,$query) or die('<div>Problem in executing the SQL query <b>,<br><br>' . $query. '</b><br><br>'.$conn->error.'</div>');
$query= '';
}
}
echo '<div>SQL file imported successfully</div>';
?>
潜在的修复
我必须在 .sql 文件的顶部添加以下行,以避免在某些 DATE 列中出现一些 DEFAULT VALUE 错误。或者,如果您收到类似的错误,您可以在执行 SQL 文件之前尝试执行以下查询。
SET GLOBAL sql_mode = 'NO_ENGINE_SUBSTITUTION';
SET SESSION sql_mode = 'NO_ENGINE_SUBSTITUTION';
此外,用更好的错误处理机制代替暴力 die() 函数。
解释
如果您愿意,我添加了一些注释行来解释该行为。
<?php
$conn = new mysqli('localhost', 'root', '' , 'db_name');
$query = ''; //Set an empty query variable to hold the query
$sqlScript = file('mySqlFile.sql'); //Set the sql file location
//Read each line of the file
foreach ($sqlScript as $line) {
//Get the starting character and the ending character of each line
$startWith = substr(trim($line), 0 ,2);
$endWith = substr(trim($line), -1 ,1);
//Check for empty or comment lines. (If the line starts with --,/*,// or the line is empty, skip to the next line)
if (empty($line) || $startWith == '--' || $startWith == '/*' || $startWith == '//') {
continue;
}
//Add the line to the query. (Additional optional commented out <br> tag added to query for easy error identification)
$query = $query . $line . "/*<br>*/";
//If the line end with a ";" assume the last query has ended in this line
if ($endWith == ';') {
//Therefore, try to execute the query. Upon failure, display the last formed query with the SQL error message
mysqli_query($conn,$query) or die('<div>Problem in executing the SQL query <b>,<br><br>' . $query. '</b><br><br>'.$conn->error.'</div>');
//Reset the query variable and continue to loop the next lines
$query= '';
}
}
//If nothing went wrong, display a success message after looping through all the lines in the sql file
echo '<div>SQL file imported successfully</div>';
/*
If failed with an invalid DEFAULT value for a DATE column error, try adding the following lines to the top of your SQL file. Otherwise, execute these lines before executing your .sql file.
SET GLOBAL sql_mode = 'NO_ENGINE_SUBSTITUTION';
SET SESSION sql_mode = 'NO_ENGINE_SUBSTITUTION';
*/
?>
原文由 Rashmitha Himath 发布,翻译遵循 CC BY-SA 4.0 许可协议
2 回答2.5k 阅读✓ 已解决
1 回答2.6k 阅读✓ 已解决
5 回答1.5k 阅读
2 回答1.1k 阅读✓ 已解决
2 回答2.2k 阅读
3 回答771 阅读✓ 已解决
1 回答857 阅读✓ 已解决
我感觉这里回答这个问题的每个人都不知道成为允许人们在自己的服务器上安装应用程序的 Web 应用程序开发人员是什么感觉。尤其是共享主机不允许您使用像前面提到的“加载数据”查询那样的 SQL。大多数共享主机也不允许您使用 shell_exec。
现在,要回答 OP,最好的办法是构建一个 PHP 文件,该文件在变量中包含您的查询,并且可以运行它们。如果您决定解析 .sql 文件,您应该查看 phpMyAdmin 并获得一些想法,以便以这种方式从 .sql 文件中获取数据。看看其他有安装程序的 Web 应用程序,你会发现,他们没有使用 .sql 文件进行查询,而是将它们打包在 PHP 文件中,然后通过 mysql_query 或任何他们需要做的事情运行每个字符串.