【发布时间】:2014-12-05 15:09:44
【问题描述】:
我在 PHP 升级方面遇到了一些问题。之前,我使用的是 PHP 5.2.0 及以下版本;现在我已经升级到 PHP 5.5.0。我的一些 sn-ps 没有按预期运行。
例如,这里有一个。它说,
已弃用:mysql_real_escape_string()
我尝试了mysqli_real_escape_string() 并得到另一个错误:
警告:mysqli_real_escape_string() 需要 2 个参数,其中 1 个给出
这是我的代码:
<?php
require_once("includes/session.php");
require_once("connections/connection.php");
require_once("includes/functions.php");
?>
<?php
$username = $_POST['username'];
$password = $_POST['password'];
//$hashed_password= md5($password);
?>
<!--Receive username password and authenticate whether the same or not with database one. -->
<?php
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($username);
$password = mysqli_real_escape_string($password);
?>
<?php
$query = "SELECT *
FROM login
WHERE username = '{$username}'
AND password = '{$password}'
AND status=1";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count == 1){
//for the session
$result_fetch= mysql_fetch_array($result);
$_SESSION['user_id']= $result_fetch['id'];
$_SESSION['user_name']= $result_fetch['username'];
session_register("username");
session_register("password");
header("Location: dashboard.php");
exit;
}
else{
echo "The username or password is incorrect.";
}
?>
<?php
//5.Close connection
if(isset($connection)){
mysql_close($connection);
}
?>
【问题讨论】:
-
请将该列表缩减为有问题的行或至少突出显示您有问题的行。
-
你的第一个问题是使用 mysql_* 函数。
-
你在哪里使用 mysqli_real_escape_string() ??
-
当尝试重构代码以支持 mysqli 而不是 mysql 时,您应该逐行执行 mysql_ 命令,并在 php 手册中的 mysqli_ 版本中查看新的/更改的语法。
-
PHP 的 mysql_ API 已弃用。请改用 PDO 或 mysqli_,并与准备好的语句结合使用。