当我在对象上调用函数时,为什么会在非对象上出现此函数调用错误?

发布于 2024-07-23 01:26:07 字数 936 浏览 3 评论 0原文

错误:

致命错误:调用成员函数 中的非对象上的bind_param() /var/www/web55/web/pdftest/events.php 第76行

代码:

public function countDaysWithoutEvents(){       
    $sql = "SELECT 7 - COUNT(*) AS NumDaysWithoutEvents
            FROM    
            (SELECT d.date 
                FROM cali_events e
                LEFT JOIN cali_dates d
                ON e.event_id = d.event_id
                WHERE YEARWEEK(d.date) = YEARWEEK(CURRENT_DATE())
                AND c.category_id = ?
                GROUP BY DAY(d.date)
            ) AS UniqueDates";
            
    $stmt = $this->link->prepare($sql);
    $stmt->bind_param('i', $this->locationID);
    $stmt->execute();
    
    $stmt->bind_result($count);
    $stmt->close();
    
    return $count;
}

$this->link->prepare($sql) 为 MySQLi 创建一条准备好的语句。

为什么我会收到此错误?

Error:

Fatal error: Call to a member function
bind_param() on a non-object in
/var/www/web55/web/pdftest/events.php
on line 76

Code:

public function countDaysWithoutEvents(){       
    $sql = "SELECT 7 - COUNT(*) AS NumDaysWithoutEvents
            FROM    
            (SELECT d.date 
                FROM cali_events e
                LEFT JOIN cali_dates d
                ON e.event_id = d.event_id
                WHERE YEARWEEK(d.date) = YEARWEEK(CURRENT_DATE())
                AND c.category_id = ?
                GROUP BY DAY(d.date)
            ) AS UniqueDates";
            
    $stmt = $this->link->prepare($sql);
    $stmt->bind_param('i', $this->locationID);
    $stmt->execute();
    
    $stmt->bind_result($count);
    $stmt->close();
    
    return $count;
}

$this->link->prepare($sql) creates a prepared statement for MySQLi.

Why am I getting this error?

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

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

发布评论

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

评论(3

神仙妹妹 2024-07-30 01:26:07

AND c.category_id = ? - 您的查询中没有表别名 c。

除此之外尝试

$stmt = $this->link->prepare($sql);
if (!$stmt) {
  throw new ErrorException($this->link->error, $this->link->errno);
}

if (!$stmt->bind_param('i', $this->locationID) || !$stmt->execute()) {
  throw new ErrorException($stmt->error, $stmt->errno);
}

AND c.category_id = ? - there is no table alias c in your query.

Besides that try

$stmt = $this->link->prepare($sql);
if (!$stmt) {
  throw new ErrorException($this->link->error, $this->link->errno);
}

if (!$stmt->bind_param('i', $this->locationID) || !$stmt->execute()) {
  throw new ErrorException($stmt->error, $stmt->errno);
}
两仪 2024-07-30 01:26:07

我认为问题显然出在 prepare 函数上。

该函数可能会失败,在这种情况下 $stmt 将为 FALSE,因此没有 bind_param 方法作为成员。

来自 php mysqli 手册
mysqli_prepare() 返回一个语句对象,如果发生错误,则返回 FALSE。

检查您的查询! 也许您的 SELECT 语句有问题。 在尝试对您认为是由准备函数返回的对象执行任何成员函数之前,还要检查 FALSE。

if($stmt === FALSE)
    die("Prepare failed... ");// Handle Error Here

// Normal flow resumes here
$stmt->bind_param("i","");

编辑

我怀疑该语句可能由于子查询而出错:

SELECT d.date 
 FROM cali_events e
 LEFT JOIN cali_dates d
 ON e.event_id = d.event_id
 WHERE YEARWEEK(d.date) = YEARWEEK(CURRENT_DATE())
 AND c.category_id = ?
 GROUP BY DAY(d.date)

相反,你为什么不这样写你的查询:

public function countDaysWithoutEvents()
{
    $count = FALSE;

    $sql  = "SELECT COUNT(d.date) ";
    $sql .= " FROM cali_events e ";
    $sql .= "      LEFT JOIN cali_dates d ON e.event_id = d.event_id ";
    $sql .= " WHERE YEARWEEK(d.date) = YEARWEEK(CURRENT_DATE()) ";
    $sql .= "       AND c.category_id = ? ";
    $sql .= " GROUP BY DAY(d.date) ";

    $stmt = $this->link->prepare($sql);
    if($stmt !== FALSE)
    {                
        $stmt->bind_param('i', $this->locationID);
        $stmt->execute();
        $stmt->bind_result($count);
        $stmt->fetch();                    // I think you need to do a fetch
                                           // here to get the result data..
        $stmt->close();
    }else                                  // Or, provide your own error
        die("Error preparing Statement");  // handling here

    return (7 - $count);
}

PS我认为你还缺少一个也调用fetch..(参见上面的示例)

I think the problem is obviously with the prepare function..

The function is probably failing, in which case $stmt would be FALSE and hence not have the bind_param method as a member.

From the php mysqli manual:
mysqli_prepare() returns a statement object or FALSE if an error occurred.

Check your query! Maybe there is a problem with your SELECT statement. And also check for FALSE before trying to execute any member function on what you think is an object returned by the prepare function.

if($stmt === FALSE)
    die("Prepare failed... ");// Handle Error Here

// Normal flow resumes here
$stmt->bind_param("i","");

EDIT

I would suspect that the statement may be erroring out because of the sub-query:

SELECT d.date 
 FROM cali_events e
 LEFT JOIN cali_dates d
 ON e.event_id = d.event_id
 WHERE YEARWEEK(d.date) = YEARWEEK(CURRENT_DATE())
 AND c.category_id = ?
 GROUP BY DAY(d.date)

Instead, why don't you write your query like this:

public function countDaysWithoutEvents()
{
    $count = FALSE;

    $sql  = "SELECT COUNT(d.date) ";
    $sql .= " FROM cali_events e ";
    $sql .= "      LEFT JOIN cali_dates d ON e.event_id = d.event_id ";
    $sql .= " WHERE YEARWEEK(d.date) = YEARWEEK(CURRENT_DATE()) ";
    $sql .= "       AND c.category_id = ? ";
    $sql .= " GROUP BY DAY(d.date) ";

    $stmt = $this->link->prepare($sql);
    if($stmt !== FALSE)
    {                
        $stmt->bind_param('i', $this->locationID);
        $stmt->execute();
        $stmt->bind_result($count);
        $stmt->fetch();                    // I think you need to do a fetch
                                           // here to get the result data..
        $stmt->close();
    }else                                  // Or, provide your own error
        die("Error preparing Statement");  // handling here

    return (7 - $count);
}

P.S. I think you also had a missing a call to fetch as well.. (see example above)

骄兵必败 2024-07-30 01:26:07

$this->link->prepare 该语句不返回对象
所以它给了你错误

$this->link->prepare this statement is not returning the object
so it is giving you the error

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