升级 PHP 后我无法使用 mysql_* 函数
我在升级 PHP 时遇到了一些问题.之前,我使用的是 PHP 5.2.0 及以下版本;现在我已经升级到 PHP 5.5.0.我的一些代码段没有按预期运行.
I am having few problems with the PHP upgrade. Before, I was using PHP 5.2.0 and below; now I have upgraded to PHP 5.5.0. A few of my snippets are not running as I expected.
例如,这是一个.它说,
Here is one, for example. it says,
不推荐使用:mysql_real_escape_string()
Deprecated: mysql_real_escape_string()
我尝试了 mysqli_real_escape_string()
并得到另一个错误:
I tried mysqli_real_escape_string()
and got another error:
警告:mysqli_real_escape_string() 需要 2 个参数,其中 1 个在
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in
这是我的代码:
<?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);
}
?>
mysqli_real_escape_string
需要两个参数才能工作:
mysqli_real_escape_string
needs two arguments to work:
语法:
mysqli_real_escape_string($connection,$escapestring);
你需要给它连接变量.这看起来像
You need to give it the connection variable. This looks like
$connection=mysqli_connect("host","my_user","my_password","my_db");
您应该更新您的 PHP 知识.
另一种方法是使用数据库对象,这样您就不必每次都传入连接详细信息.
An alternative method would be to use a database object so you don’t have to pass in the connection details each time.