3

错误:

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

代码:

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 创建一个准备好的语句。

为什么我会收到此错误?

4

3 回答 3

3

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);
}
于 2009-06-05T21:14:17.000 回答
1

我认为问题显然出在准备功能上。

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

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

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)

于 2009-06-05T20:49:26.643 回答
0

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

于 2009-06-05T20:49:56.860 回答