【问题标题】:Issue with mysql_real_escape_string()mysql_real_escape_string() 的问题
【发布时间】:2012-10-25 14:25:32
【问题描述】:

几天前我开始学习编码,我遇到了 mysql_real_escape_string 的一些问题,特别是 login.php。

错误信息:

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'elegant'@'localhost' (using password: NO) in /home/elegant/public_html/php/login.php on line 3

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the     server could not be established in /home/elegant/public_html/php/login.php on line 3

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'elegant'@'localhost' (using password: NO) in /home/elegant/public_html/php/login.php on line 4

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/elegant/public_html/php/login.php on line 4
Please enter a username and a password

这是我目前的代码——这段代码在 localhost 中工作,但是一旦我把它放到网上并导入数据库表,它给了我一些问题:

<?php

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);

if ($username&&$password)

{

$connect = mysql_connect("localhost","elegant_root","password;1") or die("Couldn't             connect!");
mysql_select_db("elegant_ezworkstation") or die("Couldn't find database");

$query = mysql_query("SELECT * FROM users WHERE username=$username");

$numrows = mysql_numrows($query);

if ($numrows!=0)
{

while ($row = mysql_fetch_assoc($query))
{

    $dbusername = $row['username'];
    $dbpassword = $row['password'];

}

if ($username==$dbusername&&$password==$dbpassword)
{

    echo "You're in";

}
else
    echo "Incorrect password!";

}
else
die("That user doesn't exist");

}

else
die("Please enter a username and a password");

?>

编辑:我更改为 mysqli,但出现以下错误:

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in /home/elegant/public_html/php/login.php on line 3

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in /home/elegant/public_html/php/login.php on line 4

【问题讨论】:

  • 在调用mysql_real_escape_string之前需要mysql connection ... 放在连接后mysql_connect
  • 重写你的代码并使用mysqli_()
  • 如果您正在学习编码,请不要从任何使用 mysql 的教程中学习......寻找使用 mysqli 或(更好)PDO 的教程
  • 我想知道您使用的什么资源让您在 2012 年走上了使用 mysql_query 的道路。
  • 请不要使用mysql_* 函数编写新代码。它们不再维护,社区已经开始deprecation process。看到 red box 了吗?相反,您应该了解prepared statements 并使用PDOMySQLi。如果你不能决定哪一个,this article 会帮助你。如果你选择 PDO,here is good tutorial

标签: php mysql string escaping


【解决方案1】:

在连接到数据库后输入mysql_real_escape_string() 会正常工作。

但是,您应该切换到 mysqli 或 PDO。 MySQL 现在已弃用。 几个链接可以帮助你

  1. Moving from mysql to mysqli or pdo?
  2. mysqli or PDO - what are the pros and cons?

mysqli 和 PDO 中用于转义的等效命令分别为 mysqli_real_escape_string()PDO::quote()

正如人们所指出的,PDO 绝对是更好的选择。这是我之前写的比较 PDO 与其他人的答案。

PDO - real facts and best practice?

这样做的另一个好处是,如果您使用带有命名参数的准备好的语句,则不需要使用转义函数。

【讨论】:

  • 关于 PDO 和mysqli 的一点是,如果你使用占位符,你真的不需要直接使用 SQL 转义函数,这真的是 only 的方式安全。
  • 我对其进行了编辑,使其使用 mysqli_real_escape_string() 但现在我收到以下错误消息: 警告:mysqli_real_escape_string() 需要 2 个参数,1 个在 /home/elegant/public_html/php/login 中给出第 3 行的 .php 警告:mysqli_real_escape_string() 需要 2 个参数,第 4 行的 /home/elegant/public_html/php/login.php 中给出了 1 个
  • 请浏览官方文档here,看看你缺少什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-11
  • 2011-06-08
  • 1970-01-01
  • 1970-01-01
  • 2014-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多