mysql_real_escape_string() 期望参数 1 为字符串
require("includes/connect.php");
$result = mysql_query("SELECT * FROM entries", $link);
while ($row = mysql_fetch_array($result)) {
htmlentities($row['quotes']);
}
我试图显示数据库中的数据,但我不断收到:
Warning: mysql_real_escape_string() expects parameter 1 to be string
上述代码中是否有任何错误导致了问题?我对 PHP 很陌生,我试图了解正在发生的事情以及它为什么这样做。
connect.php
$link = mysql_connect("localhost", "root", "");
if (!$link) {
die("Could not connect to the db");
}
mysql_select_db("ENTRIES", $link);
(我正在本地处理这个问题,所以用户/密码现在并不重要)
require("includes/connect.php");
$result = mysql_query("SELECT * FROM entries", $link);
while ($row = mysql_fetch_array($result)) {
htmlentities($row['quotes']);
}
I am trying to display data that is in the database, but I keep on getting:
Warning: mysql_real_escape_string() expects parameter 1 to be string
Is there anything wrong in the above code that is causing the problem? I am quite new to PHP and I am trying to understand what's going on and why it's doing it.
connect.php
$link = mysql_connect("localhost", "root", "");
if (!$link) {
die("Could not connect to the db");
}
mysql_select_db("ENTRIES", $link);
(I'm working on this locally, so user/pass really isn't important right now)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为转义上述查询有什么意义,但您可以这样做:
您应该阅读文档: mysql_real_escape_string()
I don't see the point with escaping the above query, but you could do it like this:
You should read the documentation: mysql_real_escape_string()
正如错误所解释的,
mysql_real_escape_string()
采用字符串作为参数。在您作为评论发布的代码中,您传递的$link
不是字符串,而是数据库连接。正如@kristen所说,解决方案应该是像这样包装你的sql语句
如果在此之后你仍然收到错误,那么你必须在其他地方使用该函数。
As the error explains
mysql_real_escape_string()
takes a string as a parameter. In your code you posted as a comment you are passing$link
which isn't a string, it's a database connection.As @kristen, has said to solution should be to wrap you sql statement like so
If you are still receiving the error after this, you must be using the function elsewhere.