Php 解析mysql\u real\u escape\u string()的最佳方法:无需关闭警告通知

Php 解析mysql\u real\u escape\u string()的最佳方法:无需关闭警告通知,php,Php,为什么在我尝试登录我的页面时会出现这些警告 ??我尝试浏览,大多数论坛都建议关闭通知,但我不认为这是一种好的编程方式。我只想了解如何修复此问题或警告,下面我将介绍这些错误 Warning: mysql_real_escape_string(): Access denied for user ''@'localhost' (using password: NO) in C:\xampp\htdocs\sot\sot.php on line 12 Warning: mysql_real_escap

为什么在我尝试登录我的页面时会出现这些警告 ??我尝试浏览,大多数论坛都建议关闭通知,但我不认为这是一种好的编程方式。我只想了解如何修复此问题或警告,下面我将介绍这些错误

Warning: mysql_real_escape_string(): Access denied for user ''@'localhost' (using password: NO) in C:\xampp\htdocs\sot\sot.php on line 12

Warning: mysql_real_escape_string(): A link to the server could not be established in C:\xampp\htdocs\sot\sot.php on line 12

Warning: mysql_real_escape_string(): Access denied for user ''@'localhost' (using password: NO) in C:\xampp\htdocs\sot\soft.php on line 13

Warning: mysql_real_escape_string(): A link to the server could not be established in C:\xampp\htdocs\sot\sot.php on line 13

Warning: mysql_query(): Access denied for user ''@'localhost' (using password: NO) in C:\xampp\htdocs\soft\sot.php on line 14

Warning: mysql_query(): A link to the server could not be established in C:\xampp\htdocs\sot\sot.php on line 14

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\sot\sot.php on line 15
这是我的登录页面

<?php
session_start();
include_once 'dbconfig.php';

if(isset($_SESSION['user'])!="")
{
    header("Location: admin.php");
}

if(isset($_POST['btn-login']))
{
    $email = mysql_real_escape_string($_POST['email']);
    $upass = mysql_real_escape_string($_POST['pass']);
    $res=mysql_query("SELECT * FROM users WHERE email='$email'");
    $row=mysql_fetch_array($res);

    if($row['password']==md5($upass))
    {
        $_SESSION['user'] = $row['user_id'];
        header("Location: admin.php");
    }
    else
    {
        ?>
        <script>alert('wrong details');</script>
        <?php
    }

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ALEXORG</title>
<link rel="stylesheet" href="css/bootstrap.css" type="text/css" />
<link rel="stylesheet" href="style.css" type="text/css" />

</head>
<body id="login">
 <div class="container">

      <form class="form-signin" method="post">
        <h2 class="form-signin-heading text-center">Muligence 1.0</h2>
        <label for="inputEmail" class="sr-only">Email address</label>
        <input type="text" name="email" id="inputEmail" class="form-control" placeholder="Enter Your email" required autofocus>
        <label for="inputPassword" class="sr-only">Password</label>
        <input type="password"  name="pass"  id="inputPassword" class="form-control" placeholder="Password" required>
        <div class="checkbox">
          <label>
            <input type="checkbox" value="remember-me"> Remember me
          </label>
        </div>

        <button class="btn btn-lg btn-primary btn-block" type="submit" name="btn-login">Sign in</button>
        <a href="register.php">Sign Up Here</a>
      </form>

    </div> <!-- /container -->




<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="js/bootstrap.js"></script>

</body>
</html>

警报(“错误的详细信息”);
阿列克索格
多源1.0
电子邮件地址
暗语
记得我吗
登录
我是编程和php新手,我希望通过建议或链接学习帮助,在那里我可以找到有用的信息 这是我的dbconfig代码

<?php

$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "ca@19";
$DB_name = "zack";


try
{
    $DB_con = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
    $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
    echo $e->getMessage();
}

include_once 'clo.crud.php';

$crud = new crud($DB_con);

?>

您正在使用PDO连接到数据库,但随后尝试使用ext/mysql(既不兼容又存在)来转义数据

您需要选择一个数据库API(PDO是一个不错的选择)并坚持使用它

演示如何使用PDO转义数据


是的,在尝试使用
mysql\u real\u escape\u字符串之前先连接到数据库。该问题是因为无法建立数据库连接。Oh和
mysql_*
函数在PHP5.5中不推荐使用,您应该使用
mysqli_*
替代方法。您可能正在连接mysqli_*
或PDO,谁知道呢。可能是任何东西。将数据库连接传递给转义函数。如果(isset($\u会话['user'])!=“”)将永远无法工作,则此
将永远无法工作。无效语法。请向我们显示您的
dbconfig.php
代码
$stmt = $pdo->prepare('SELECT * FROM employees WHERE name = :name');
$stmt->execute(array('name' => $name));