dpsu84620 2016-05-13 20:15
浏览 146

mysql真正的转义字符串解决sql注入绝对[重复]

This question already has an answer here:

I want to know if I add mysql_real_escape_string to my variables that's enough to solve sql injection

$get_id = "select * from `book` where id='".$mysqli->real_escape_string($id)."' limit 1";
</div>
  • 写回答

1条回答 默认 最新

  • dongxie3681 2016-05-13 20:16
    关注

    No, it isn't. Use prepared statements.

    You would have to do something like this:

    // Your connection settings
    $connData = ["localhost", "user", "pass", "database"];
    
    $conn = new mysqli($connData[0], $connData[1], $connData[2], $connData[3]);
    $conn->set_charset("utf8");
    
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    // Here we explain MySQL which will be the query
    $stmt = $conn->prepare("select * from book where id=? limit 1");
    
    // Here we tell PHP which variable hash de "?" value. Also you tell PHP that $id has an integer ("i")
    $stmt->bind_param("i", $id);
    
    // Here we bind the columns of the query to PHP variables
    $stmt->bind_result($column1, $column2, ...); // <--- Whichever columns you have
    
    // Here we execute the query and store the result
    $stmt->execute();
    $stmt->store_result();
    
    // Here we store the results of each row in our PHP variables ($column1, column2, ...)
    while($stmt->fetch()){
        // Now we can do whatever we want (store in array, echo, etc)
        echo "<p>$column1 - $column2 - ...</p>";
    }
    
    $stmt->close();
    $conn->close();
    
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊