I cannot use mysql_ * functions after PHP update
I am having trouble updating PHP. Previously, I used PHP 5.2.0 and below; I have now upgraded to PHP 5.5.0. Some of my snippets are not working as I expected.
Here's one, for example. He says,
Deprecated: mysql_real_escape_string ()
I tried mysqli_real_escape_string()
and got another error:
Warning: mysqli_real_escape_string () expects exactly 2 parameters, 1 given in
Here is my code:
<?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);
}
?>
-3
source to share
1 answer
mysqli_real_escape_string
two arguments are needed:
Syntax:
mysqli_real_escape_string($connection,$escapestring);
You need to specify a connection variable. It looks like
$connection=mysqli_connect("host","my_user","my_password","my_db");
You should update your PHP knowledge .
An alternative method would be to use a database object, so you don't have to pass the connection data every time.
+2
source to share