douxun1407 2015-06-01 18:23
浏览 47
已采纳

mysqli_query没有按预期工作

I have the php:

$con=mysqli_connect("127.0.0.1","foo","bar","quaz");
if($con){
    $sql = "INSERT INTO `virtual_users` (`domain_id`, `password` , `email`) 
    VALUES ('2', ENCRYPT('".$password."', CONCAT('\$6\$', SUBSTRING(SHA(RAND()), -16))), '".$user."@".$domain."');";

    $query = mysqli_query($con, $sql);

    if(!$query){
        echo $sql;
    }else{
        echo "Success adding new email user!";
    }
}

For some reason when I run this query it always returns $sql. This means that the connection is fine but the query is not.

When I then run the the echo of $sql directly on the mysql database it works perfectly!! I have no idea what is going wrong! Any ideas?

  • 写回答

1条回答 默认 最新

  • dsbo44836129 2015-06-01 18:26
    关注

    That's because you should be outputting the reason for the failure, not the query that caused the failure:

    $result = mysqli_query($con, $sql) or die(mysqli_error($con));
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    There's exactly ONE way for a query to succeed, and a near infinite number of ways for it to fail. Just having "valid" sql means nothing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法