Why do I get this function call error on an non-object when I am calling a function on an object?

前端 未结 3 813
野的像风
野的像风 2021-01-20 08:04

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

Co

相关标签:
3条回答
  • 2021-01-20 08:36

    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);
    }
    
    0 讨论(0)
  • 2021-01-20 08:45

    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)

    0 讨论(0)
  • 2021-01-20 08:51

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

    0 讨论(0)
提交回复
热议问题