简体   繁体   English

mysqli_real_escape_string()返回空字符串

[英]mysqli_real_escape_string() returning empty string

anyone have this solution??i am having this problem from this evening after sudden blue screen error. 谁有这个解决方案?我从今天晚上突然蓝屏错误后就遇到了这个问题。 I was using this simple code to implement session in project something worked but after that mysterious restart this thing is acting weird!!!! 我当时在用这个简单的代码在项目中实现会话,但有些成功,但是在神秘重启之后,这件事变得很奇怪!

This code is supposed to work!!!! 该代码应该可以正常工作!!!

UPDATE: Took care of some minor mistakes and its fully operational now :) 更新:照顾了一些小错误,并且现在可以完全操作了:)

    <?php
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
    if (empty($_POST['username']) || empty($_POST['password'])) {
        $error = "Username or Password is invalid";
        }
        else
        {
            // Define $username and $password
            $username=$_POST['username'];
            $password=$_POST['password'];
            // Establishing Connection with Server by passing server_name, user_id and password as a parameter
            $connection = mysqli_connect("localhost", "root", "root","company");// Selecting Database
            if (!$connection) {
                die("Connection failed: " . mysqli_connect_error());
                }
                // To protect MySQL injection for Security purpose
                //$username = stripslashes($username);
                //$password = stripslashes($password);

                $username = mysqli_real_escape_string($connection,$username);
                $password = mysqli_real_escape_string($connection,$password);



                // SQL query to fetch information of registerd users and finds user match.
                $query = "select * from login where password='$password' AND username='$username'";  

                //$query = "select * from login where password='".$password."' AND username='".$username."'";



                $result=mysqli_query($connection,$query);
                $rows = mysqli_num_rows($result);


                if ($rows == 1) {
                    $_SESSION['login_user']=$username; // Initializing Session
                    header("location: profile.php"); // Redirecting To Other Page
                    }
                    else {
                        $error = "Username or Password Invalid";
                        }
                        mysqli_close($connection); // Closing Connection
                        }
                        }
?>

Output image(before editing) - 输出图像(编辑前)-

输出图像

mysqli_real_escape_string($username) is missing a required parameter mysqli_real_escape_string($username)缺少必需的参数

The usage of mysqli_real_escape_string() function is following: mysqli_real_escape_string()函数的用法如下:

$con=mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$age = mysqli_real_escape_string($con, $_POST['age']);

You have to add a $connection (your connection variable). 您必须添加$ connection(您的连接变量)。 So that it becomes: mysqli_real_escape_string($connection, $username) 这样它就变成了: mysqli_real_escape_string($connection, $username)

http://www.w3schools.com/php/func_mysqli_real_escape_string.asp http://www.w3schools.com/php/func_mysqli_real_escape_string.asp

This is the escape string that I use 这是我使用的转义字符串

<?php
if (!function_exists('escapestring')) {

function escapestring($vconnection, $value, $datatype) { 

  $value = function_exists('mysqli_real_escape_string') ? mysqli_real_escape_string($vconnection, $value) : mysqli_escape_string($vconnection, $value);

  switch ($datatype) { 
    case 'text':
      $value = ($value != '') ? "'" . $value . "'" : "'na'";
      break;      
    case 'int':
      $value = ($value != '') ? intval($value) : "'na'";
      break;  
    case 'float':
      $value = ($value != '') ? floatval($value) : "'na'";
      break;
    case 'date':
      $value = ($value != '') ? "'" . $value . "'" : "'na'";
      break; 
    }
    return $value;
    }
}
?>

Your query is not using quotes properly it will take '$password' and '$username' as it is instead of their values, change it to this- 您的查询未正确使用引号,它将使用'$password''$username' ,而不是它们的值,请将其更改为-

$query = "select * from login where password='".$password."' AND username='".$username."'";

Instead of this 代替这个

$query = "select * from login where password='$password' AND username='$username'";

plus you need to use mysqli_close() instead of mysql_close() and mysqli_real_escape_string($connection, $username) as suggested by pavlovich . 再加上你需要使用mysqli_close()代替mysql_close()mysqli_real_escape_string($connection, $username)帕夫洛维奇的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM