升级 PHP 后我无法使用 mysql_* 函数 [英] I cannot use mysql_* functions after upgrading PHP

查看:23
本文介绍了升级 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.

这篇关于升级 PHP 后我无法使用 mysql_* 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆