升级PHP后我无法使用mysql_ *函数

时间:2014-10-10 12:35:01

标签: php mysql session

我在PHP升级方面遇到的问题很少。之前,我使用的是PHP 5.2.0及以下版本;现在我已升级到PHP 5.5.0。我的一些片段没有像我预期的那样运行。

例如,这是一个。它说,

  

不推荐使用: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);
 }

?>

1 个答案:

答案 0 :(得分:3)

mysqli_real_escape_string需要两个参数才能工作:

语法:

mysqli_real_escape_string($connection,$escapestring);

您需要为其提供连接变量。这看起来像

$connection=mysqli_connect("host","my_user","my_password","my_db");

你应该refresh your PHP knowledge

另一种方法是使用数据库对象,这样您就不必每次都传递连接细节。