升级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.