PHP 和 mysql 不断出错。调用未定义的函数

我已经为登录和数据库制作了一个 PHP 文件,但不断出现错误


致命错误:Uncaught Error: Call to undefined function mysql_real_escape_string() in C:\wamp64\www\Login\process.php on line 15


错误:在第 15 行调用 C:\wamp64\www\Login\process.php 中未定义的函数 mysql_real_escape_string()


代码开始


<?php 

  // to get values passe from form in login.php file

  $username = $POST['username']

  $password = $POST['password']

  

  // to prevent mysql injection

  $username = stripcslashes($username);

  $password = stripcslashes($password);

  $username = mysql_real_escape_string($username);

  $password = mysql_real_escape_string($username);


  //connect to the server select database

  mysql_connect("localhost", "root", "");

  mysql_select_db("login");


  // Query the database for user

  $result = mysql_query("select * from users where username = '$username' and password = '$password'")

    or die('Failed to query database'.mysql_error());

  $row = mysql_fetch_array($result);

  if ( $row['username'] == $username && $row['password'] == $password ){

      echo "login success! Welcome".$row['username'];

  } else {

    echo "Failed to login!";

}


?>


慕运维8079593
浏览 304回答 1
1回答

杨__羊羊

看来您正在使用最新版本的 PHP。这个扩展在 PHP 5.5.0 中被弃用,在 PHP 7.0.0 中被移除。相反,应使用 MySQLi 或 PDO_MySQL 扩展。我的建议是使用 mysqli_*$con = new mysqli("localhost", "root", "", "login");$username = isset($_POST['username']) ? $_POST['username'] : '';$password = isset($_POST['password']) ? $_POST['password'] : '';// Query the database for user$stmt = $con->prepare("SELECT * FROM users WHERE username = ? AND password = ?");$stmt->bind_param("ss", $username, $password);$stmt->execute();$res = $stmt->get_result();$row = $res->fetch_assoc();if ( $row['username'] == $username && $row['password'] == $password ){&nbsp; &nbsp; echo "login success! Welcome".$row['username'];} else {&nbsp; &nbsp;echo "Failed to login!";}
打开App,查看更多内容
随时随地看视频慕课网APP