php 警告:mysql_real_escape_string():用户 ''@'localhost' 访问被拒绝(使用密码:NO)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17870024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: mysql_real_escape_string(): Access denied for user ''@'localhost' (using password: NO)
提问by Dennis1973
When is use the following code without mysql_real_escape_string, works fine. I simply trying to grab a text string that may have apost. from an input form and format it to put in mysql table.
什么时候在没有 mysql_real_escape_string 的情况下使用以下代码,效果很好。我只是想抓取一个可能有 apost 的文本字符串。从输入表单并将其格式化以放入 mysql 表中。
<?php
$filenamee = $_FILES["file"]["name"];
$filename =strval($filenamee);
echo "file name is".$filename;
$con=mysqli_connect("localhost","blasbott_admin","lubu1973","blasbott_upload");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$companyName = mysql_real_escape_string($_POST['companyName']);
// $companyName = mysql_real_escape_string($companyNamee);
//$companyName = mysql_real_escape_string($companyNamee);
$sql="INSERT INTO ads (companyName, webSite, picture)
VALUES ('$companyName','$_POST[webSite]','$filename')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo"<br>";
echo "1 record added";
mysqli_close($con);
?>
回答by bwoebi
No, you shouldn't mix mysql and mysqli.
不,你不应该混合使用 mysql 和 mysqli。
Use here instead of mysql_real_escape_string($var)
:
在这里使用而不是mysql_real_escape_string($var)
:
$con->real_escape_string($var);
回答by Adrian.S
You're at risk of MySQL injections. Never insert data directly to a database without some sort of projection first. It's a major security risk. Also use mysqli_real_escape_string instead, and note that your $_POST[webSite] is unprotected.
您面临 MySQL 注入的风险。切勿在没有先进行某种投影的情况下将数据直接插入到数据库中。这是一个重大的安全风险。也请改用 mysqli_real_escape_string,并注意您的 $_POST[webSite] 不受保护。
Also, your error means that your database details are not correct.
此外,您的错误意味着您的数据库详细信息不正确。
回答by Maija Vilkina
A working code example that is a solution to this problem is available here:
可在此处找到解决此问题的工作代码示例:
http://www.w3schools.com/php/func_mysqli_real_escape_string.asp
http://www.w3schools.com/php/func_mysqli_real_escape_string.asp
回答by frozenfire
You must connect with database first before using Warning: mysql_real_escape_string()
在使用警告:mysql_real_escape_string() 之前,您必须先连接数据库
回答by ygesher
Try mysqli_real_escape_string
instead.
试试吧mysqli_real_escape_string
。