如何解决mysql_real_escape_string()函数中的错误?

时间:2016-10-26 03:06:48

标签: php mysql mysqli

  

致命错误:未捕获错误:在C:\ xampp \ htdocs \ MyFirstWebsite \ register.php中调用未定义函数mysql_real_escape_string():19堆栈跟踪:#0 {main}抛出C:\ xampp \ htdocs \ MyFirstWebsite \第19行的register.php

   <?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    echo "Username entered is : ". $username . "<br/>";
    echo "Password entered is : ". $password;
}
?>

第19行的代码如下:

$username = mysql_real_escape_string($_POST['username']);

然后我尝试使用mysqli()。所以我改变了我的PHP代码:

<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $username = mysqli_real_escape_string($_POST['username']);
    $password = mysqli_real_escape_string($_POST['password']);
    echo "Username entered is : ". $username . "<br/>";
    echo "Password entered is : ". $password;
}
?>

Then it shows this output:

  

警告:mysqli_real_escape_string()只需要2个参数,1   在第19行的C:\ xampp \ htdocs \ MyFirstWebsite \ register.php中给出

     

警告:mysqli_real_escape_string()只需要2个参数,1   在第20行的C:\ xampp \ htdocs \ MyFirstWebsite \ register.php中给出

这里,在第19行和第20行。以下代码在于:

$username = mysqli_real_escape_string($_POST['username']);
        $password = mysqli_real_escape_string($_POST['password']);

任何人都可以帮助我并告诉我该代码应该包含哪些内容?

1 个答案:

答案 0 :(得分:0)

您必须将连接变量作为第一个参数传递,如下所示:

$username = mysqli_real_escape_string($conn,$_POST['username']);

此处$conn

$conn = mysqli_connect('hostname','username','password','dbname');