出于某种原因,我有一些 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 中时,它就会成功执行。
任何帮助将不胜感激,谢谢!:)