PHP MYSQL回车

PHP MYSQL回车,php,Php,我有一个备份wordpress数据库的脚本,它工作得很好。然而,我试图用php来恢复它,它总是在回车时停止,然后出错。 错误: \n\n这是一个文本示例,在第1行更改 您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 靠近“\n\n” 我尝试了nl2br,htmlspecialchars,mysql\u real\u escape\u string,甚至preg替换了它们 以下是我使用的代码: // Connect to MySQL server mysql_c

我有一个备份wordpress数据库的脚本,它工作得很好。然而,我试图用php来恢复它,它总是在回车时停止,然后出错。 错误:

\n\n这是一个文本示例,在第1行更改

您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 靠近“\n\n”

我尝试了
nl2br
htmlspecialchars
mysql\u real\u escape\u string
,甚至
preg替换了它们

以下是我使用的代码:

// Connect to MySQL server
mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
// Select database
mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());

// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename, FILE_IGNORE_NEW_LINES);
// Loop through each line
foreach ($lines as $line)
{
    // Skip it if it's a comment
    if (substr($line, 0, 2) == '--' || $line == '')
        continue;

    // Add this line to the current segment
    $templine .= $line;
    // If it has a semicolon at the end, it's the end of the query
    if (substr(trim($line), -1, 1) == ';')
    {
        // Perform the query
        mysql_query($templine) or print(mysql_error() . '<br /><br />');
        // Reset temp variable to empty
        $templine = '';
       }
}
//连接到MySQL服务器
mysql_connect($mysql_host,$mysql_username,$mysql_password)或die('Error connecting to mysql server:'.mysql_Error());
//选择数据库
mysql_选择_db($mysql_database)或die('Error selection mysql database:'。mysql_Error());
//临时变量,用于存储当前查询
$templine='';
//读取整个文件
$lines=file($filename,file\u IGNORE\u NEW\u行);
//每行循环一次
foreach($line作为$line)
{
//如果是评论,请跳过它
if(substr($line,0,2)='--'| |$line='')
继续;
//将此行添加到当前线段
$templine.=$line;
//如果它的末尾有一个分号,那么它就是查询的结尾
if(substr(修剪($line),-1,1)=';')
{
//执行查询
mysql_查询($templine)或打印(mysql_error()。

); //将温度变量重置为空 $templine=''; } }

$filename
变量是格式正确的
.sql
文件。在数据库转储中,我可以看到\n的,我非常确定
wordpress
做了某种
nl2br
,并将它们替换为
,因此可以删除它们或完全删除站点上的间距。

在运行查询之前回送$templine。除非$filename包含有效的mysql,否则您不是在做我认为您认为您正在做的事情。请使用mysqli或PDO,而不是mysql。回显$templine显示有效的mysql,我看到了\n。$filename是一个.sql文件,它来自phpmyadmin的转储文件,因此它必须有效。如果出现错误,则它将无效,因此…
\n
可能被PHP解释为换行符。你有没有试着摆脱后斜线?