Why am I getting this function call error for a non-object when I call a function on an object?

Error:

Fatal error: call of bind_param () member function on non-object in / var / www / web 55 / web / pdftest / events.php on line 76

The 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?

+3
source share
3 answers

AND c.category_id = ? - your request does not have an alias c.

Also 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);
}
+3
source

I think the problem is clearly related to preparation .

The function probably doesn't work, in which case $ stmt will be FALSE and therefore does not have the bind_param method as a member.

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);
}

P.S. , fetch (. )

+1

$this- > link- >

0

All Articles