mysql_real_escape_string() 期望参数 1 为字符串

发布于 2024-12-26 15:22:54 字数 668 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻找一个思念的角度 2025-01-02 15:22:56

我不认为转义上述查询有什么意义,但您可以这样做:

 $result = mysql_query(mysql_real_escape_string("SELECT * FROM entries"), $link);

您应该阅读文档: mysql_real_escape_string()

I don't see the point with escaping the above query, but you could do it like this:

 $result = mysql_query(mysql_real_escape_string("SELECT * FROM entries"), $link);

You should read the documentation: mysql_real_escape_string()

夏见 2025-01-02 15:22:56

正如错误所解释的,mysql_real_escape_string() 采用字符串作为参数。在您作为评论发布的代码中,您传递的 $link 不是字符串,而是数据库连接。

正如@kristen所说,解决方案应该是像这样包装你的sql语句

$result = mysql_query(mysql_real_escape_string("SELECT * FROM entries"), $link);

如果在此之后你仍然收到错误,那么你必须在其他地方使用该函数。

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

$result = mysql_query(mysql_real_escape_string("SELECT * FROM entries"), $link);

If you are still receiving the error after this, you must be using the function elsewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文