MySQLi 可捕获的致命错误

发布于 2024-08-24 03:20:34 字数 948 浏览 6 评论 0原文

我正在使用准备好的语句和 MySQLi,目前遇到此错误。

PHP Catchable fatal error:  Object of class mysqli_stmt could not be converted to string in /Users/me/path/to/project/ModelBase/City.php on line 31

它来自的代码(完整功能):

    function select($query, $limit)
{
    $query = "%".$query."%";
    $con = ModelBase::getConnection();
    $sql = "SELECT name FROM cities WHERE name LIKE ? LIMIT ?";
    $query = $con->prepare($sql) or die("Error preparing sql in City ".parent::$db_conn->error);
    $query->bind_param("si", $query, $limit) or die("Error binding params in City ".parent::$db_conn->error);
    $query->execute() or die("Error executing query in City");

    $tmp = "";
    $res = $query->bind_result($tmp);
    while($query->fetch());
    {
        $citylist[] = $tmp;
    }
    $query->close();
}

像 31 一样是 $query->execute()。我找不到任何有关此问题的信息,这与我构建的其他系统几乎相同的语法,但从未遇到过此问题。

I'm using prepared statements and MySQLi, and am currently getting this error.

PHP Catchable fatal error:  Object of class mysqli_stmt could not be converted to string in /Users/me/path/to/project/ModelBase/City.php on line 31

The code it's coming from (full function):

    function select($query, $limit)
{
    $query = "%".$query."%";
    $con = ModelBase::getConnection();
    $sql = "SELECT name FROM cities WHERE name LIKE ? LIMIT ?";
    $query = $con->prepare($sql) or die("Error preparing sql in City ".parent::$db_conn->error);
    $query->bind_param("si", $query, $limit) or die("Error binding params in City ".parent::$db_conn->error);
    $query->execute() or die("Error executing query in City");

    $tmp = "";
    $res = $query->bind_result($tmp);
    while($query->fetch());
    {
        $citylist[] = $tmp;
    }
    $query->close();
}

Like 31 is the $query->execute(). I can't find any information about this, and this is almost identical syntax to other systems I've built and never had this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你与清晨阳光 2024-08-31 03:20:34

我认为问题在于您在两个上下文中使用 $query 。一次作为 SQL 字符串,一次准备语句:

$query = "%".$query."%";
...
$query = $con->prepare($sql) 

因此当您运行此行时:

 $query->bind_param("si", $query <--- bind to itself?

实际查询的执行将崩溃。

I think the problem is that you are using $query in two contexts. Once as a SQL string, and once to prepare your statement:

$query = "%".$query."%";
...
$query = $con->prepare($sql) 

so when you run this line:

 $query->bind_param("si", $query <--- bind to itself?

execution of the actual query will crash.

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