mySQL插入在PHP中失败但在phpmyadmin中失败

时间:2013-08-11 11:49:00

标签: php mysql mysqli phpmyadmin syntax-error

出于某种原因,我有一些SQL插件在通过php执行时失败,但是直接粘贴到phpmyadmin时却没有。

这是PHP代码:

private function createMockAccount($email){
    $passwordHash=$this->nonce();
    $query='insert into GeneralUser(email,password,isTemp) values("'.$email.'","'.$passwordHash.'",1);';
    $query=$query.'insert into ContentUser(email) values("'.$email.'");';
    error_log("executing the following query to create mock accounts: ".$query);
    $database=mysqli_connect(host,username,password,dbName);
    if (mysqli_connect_errno()) {
         error_log("Connect failed: ".mysqli_connect_error());
    }
    if(!mysqli_query($database,$query)){
     error_log("Errormessage:".mysqli_error($database));    
    }
    return $passwordHash;
}

执行时,以下内容位于错误日志中:

[11-Aug-2013 04:20:42 America/Denver] executing the following query to create mock accounts: insert into GeneralUser(email,password,isTemp) values("suxhfisk@guerrillamail.biz","D0Obkg/Lue+4AFSAzinqdo/XAAwDhMfitmnm53R0RwA=",1);
        insert into ContentUser(email) values("suxhfisk@guerrillamail.biz");
[11-Aug-2013 04:20:42 America/Denver] Errormessage:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'insert into ContentUser(email) values("suxhfisk@guerrillamail.biz")' at line 2

我也尝试过以下方式使用mysqli_real_escape_string无效:

$query=mysqli_real_escape_string($database,$query);

这样,error_log仍会显示相同的错误......

[11-Aug-2013 04:51:42 America/Denver] executing the following query to create mock accounts: insert into GeneralUser(email,password,isTemp) values("suxhfisk@guerrillamail.de","guR8Sps8e4Iv1LBXmsREH2GVd+WH/cH1Nx/zy9VOnLE=",1);
        insert into ContentUser(email) values("suxhfisk@guerrillamail.de");
[11-Aug-2013 04:51:42 America/Denver] Errormessage:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\"suxhfisk@guerrillamail.de\",\"guR8Sps8e4Iv1LBXmsREH2GVd+WH/cH1Nx/zy9VOnLE=\",1' at line 1

我确定我有一些愚蠢的东西,但是在这两种情况下,当error_log中的字符串被直接复制到phpmyadmin时,它会被成功执行。

非常感谢任何帮助,谢谢! :)

2 个答案:

答案 0 :(得分:0)

这不是您正在运行的查询,而是设置查询 所以,只需逐个运行它们,而不是在一个声明中。

另外,您使用mysqli_real_escape_string的想法是非常错误的。此函数必须仅用于转义字符串文字中的特殊字符,而不是处理整个查询。

无论如何,您必须使用预备语句。由于它的使用与原始mysqi相当复杂,所以请考虑使用PDO

答案 1 :(得分:-1)

phpmyadmin查询窗口将每个新行(因此在&#39 ;;')标记之后处理为一个任务。通过这种方式,它可以一次运行多个查询。如果要在PHP中完成相同的操作,则必须使用事务。以下是一个例子,

try {
    // start transaction
    $db->beginTransaction();
    // do 1st query 
    $db->query("insert into GeneralUser(email,password,isTemp) values('" . $email . "', '" . $passwordHash . "',1);");
    // do 2nd query
    $db->query("insert into ContentUser(email) values('" . $email . "');");
    // commits the operation
    $db->commit();
}
catch(Exception $e) {
    // either echo or log the error message
    // rollback the above successful query (optional, but depends of your design)
    $db->rollback();
}

或者改为使用PDO