By Malfist


2009-06-05 20:43:54 8 Comments

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?

3 comments

@VolkerK 2009-06-05 21:14:17

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

@Powerlord 2009-06-05 21:17:59

+1: I didn't notice c was missing when I read through it. Whoops.

@VolkerK 2009-06-05 21:27:38

The query has a certain "smell" anyway, at least it seems suboptimal if not wrong ;-)

@Malfist 2009-06-08 13:20:08

AH! Duh! It should be e.category_id. headdesk

@Mike Dinescu 2009-06-05 20:49:26

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)

@Malfist 2009-06-05 20:51:58

but the query executes fine in the console

@Mike Dinescu 2009-06-05 20:54:41

well, that may be, but that doesn't mean that MySQLi can't complain about something. I know there are some limitations with executing MySQL code from PHP mostly for preventing SQL injection.. mostly regarding subqueries..

@Malfist 2009-06-05 20:58:13

how can I correct it, that sub query really needs to be there.

@Malfist 2009-06-08 13:08:00

no, they query returns an inaccurate result. I'm counting the number of days that do not have events attached to them.

@maxjackie 2009-06-05 20:49:56

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

Related Questions

Sponsored Content

4 Answered Questions

[SOLVED] How to use mysqli prepared statements?

26 Answered Questions

[SOLVED] How do I get PHP errors to display?

36 Answered Questions

[SOLVED] Reference - What does this error mean in PHP?

13 Answered Questions

[SOLVED] Why shouldn't I use mysql_* functions in PHP?

  • 2012-10-12 13:18:39
  • Madara's Ghost
  • 225877 View
  • 2530 Score
  • 13 Answer
  • Tags:   php mysql

1 Answered Questions

[SOLVED] Error php sql: Call to a member function bind_param() on boolean

  • 2017-11-26 21:26:08
  • Marco Giuseppini
  • 76 View
  • 1 Score
  • 1 Answer
  • Tags:   php mysql sql mysqli

1 Answered Questions

[SOLVED] Issue with "Call to member function on non-object"

Sponsored Content