我有一个PHP脚本应该执行几个mysql语句,如果没有注释/ * * /并且没有分隔线,一切正常...
是否可以帮助我添加此功能,同时忽略--
条评论
<?
$sqlFileToExecute = 'mysql_dump.sql';
$hostname = 'localhost';
$db_user = 'root';
$db_password = '';
$database_name = 'db_';
$link = mysql_connect($hostname, $db_user, $db_password);
if (!$link) {
die ("error connecting MySQL");
}
mysql_select_db($database_name, $link) or die ("wrong DB");
$f = fopen($sqlFileToExecute,"r+");
$sqlFile = fread($f, filesize($sqlFileToExecute));
$sqlArray = explode(';',$sqlFile);
foreach ($sqlArray as $stmt) {
//THIS SEEMS NOT TO WORK
if (strlen($stmt)>3 && substr(ltrim($stmt),0,2)!='/*') {
$result = mysql_query($stmt);
if (!$result) {
$sqlErrorCode = mysql_errno();
$sqlErrorText = mysql_error();
$sqlStmt = $stmt;
break;
}
}
}
if ($sqlErrorCode == 0) {
echo "SETUP COMPLETED ;)";
} else {
echo "FAIL!<br/>";
echo "Error code: $sqlErrorCode<br/>";
echo "Error text: $sqlErrorText<br/>";
echo "Statement:<br/> $sqlStmt<br/>";
}
?>
答案 0 :(得分:1)
您确定需要以这种方式运行它:
您也可以使用
$mysql -pxxx -u username db_name -vvv < sourcefile.sql >/tmp/outfile.log
启用详细模式以检查统计数据,否则您可以省略-vvv选项
OR
mysql > source "sourcefile.sql"
您可以看到更多选项here