Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Fatal Error: Call to a prepare function

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Fatal Error: Call to a prepare function

Postby AngelGirl88 » Mon Jul 01, 2013 5:35 am

I've been re-reading a PHP and JQuery book that gives an events calendar as the internal project. I've successfully built this calendar once before and thought I'd try it again. However when I go to load the events data I get the error Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\calendar\sys\class\class.calendar.inc.php on line 158.

The calendar class essentially "Builds and manipulates an events calendar" through multiple functions. There is also an event class that stores event information.

The calendar class is as follows:
Code: Select all
<?php

/**
 * Builds and manipulates an events calendar
 *
 * PHP version 5
 *
 * @author      Candice Chapman
 * @copyright   2013 Holy Trinity and Saint James
 */
class Calendar extends DB_Connect
{
   /**
    * The date from which the calendar should be built
    *
    * Stored in YYYY-MM-DD HH:MM:SS format
    *
    * @var string the date to use for the calendar
    */
   private $_useDate;
   
   /**
    * The month for which the calendar is being built
    *
    * @var int the month being used
    */
   private $_m;
   
   /**
    * The year from which the month's start day is selected
    *
    * @var int the year being used
    */
   private $_y;
   
   /**
    * The number of days in the month being used
    *
    * @var int the number of days in the month
    */
   private $_daysInMonth;

   /**
    * The index of the day of the week the month starts on (0-6)
    *
    * @var int the day of the week the month starts on
    */
   private $_startDay;

 /**
  * Creates a database object and stores relevant data
  *
  * Upon instantiation, this class accepts a database object
  * that, if not null, is stored in the object's private $_db
  * property. If null, a new PDO object is created and stored
  * instead.
  *
  * Additional info is gathered and stored in this method,
  * including the month from which the calendar is being built,
  * how many days are in said month, what day the month starts
  * on, and what day it is currently.
  *
  * @param object $dbo a database object
  * @param string $useDate the date to use to build the calendar
  * @return void
  */
 public function __construct($dbo=NULL, $useDate=NULL)
 {
   /*
    * Call the parent constructor to check for
    * a database object
    */
   parent::__construct($dbo);
   
   /*
    * Gather and store data relevant to the month
    */
   if ( isset($useDate) )
   {
      $this->_useDate = $useDate;
   }
   else
   {
      $this->_useDate = date('Y-m-d H:i:s');
   }
   
   /*
    * Convert to a timestamp, then determine the month
    * and year to use when building the calendar
    */
   $ts = strtotime($this->_useDate);
   $this->_m = date('m', $ts);
   $this->_y = date('Y', $ts);
   
   /*
    * Determine how many days are in the month
    */
   $this->_daysInMonth = cal_days_in_month(
         CAL_GREGORIAN,
         $this->_m,
         $this->_y
      );
   
   /*
    * Determine what weekday the month starts on
    */
   $ts = mktime(0, 0, 0, $this->_m, 1, $this->_y);
   $this->_startDay = date('w', $ts);
 }

 /**
  * Loads events(s) info into an array
  *
  * @param int $id an optional event ID to filter results
  * @return array an array of events from the database
  */
 private function _loadEventData($id=NULL)
 {
   $sql = "SELECT
            `event_id`, `event_title`, `event_desc`,
            `event_start`, `event_end`
         FROM `james_events`";
   
   /*
    * If an event ID is supplied, add a WHERE clause
    * so only that event is returned
    */
   if ( !empty($id) )
   {
      $sql .= "WHERE `event_id`=:id LIMIT 1";
   }
   
   /*
    * Otherwise, load all events for the month in use
    */
   else
   {
      /*
       * Find the first and last days of the month
       */
      $start_ts = mktime(0, 0, 0, $this->_m, 1, $this->_y);
      $end_ts = mktime(23, 59, 59, $this->_m+1, 0, $this->_y);
      $start_date = date('Y-m-d H:i:s', $start_ts);
      $end_date = date('Y-m-d H:i:s',   $end_ts);
      
      /*
       * Filter events to only those happening in the
       * currently selected month
       */
      $sql .= "WHERE `event_start`
               BETWEEN '$start_date'
               AND '$end_date'
            ORDER BY `event_start`";
   }
   
   try
   {
      $stmt = $this->db->prepare($sql);    LINE 158
      
      /*
       * Bind the parameter if an ID was passed
       */
      if (!empty($id) )
      {
         $stmt->bindParam(":id", $id, PDO::PARAM_INT);
      }
      
      $stmt->execute();
      $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
      $stmt->closeCursor();
      
      return $results;
   }
   catch ( Exception $e )
   {
      die ( $e->getMessage() );
   }
 }

 /**
  * Loads all events for the month into an array
  *
  * @return array events info
  */
 private function _createEventObj()
 {
   /*
    * Load the events array
    */
   $arr = $this->_loadEventData();
   
   /*
    * Create a new array, then organize the events
    * by the day of the month on which they occur
    */
   $events = array();
   foreach ( $arr as $event )
   {
      $day = date('j', strtotime($event['event_start']));
      
      try
      {
         $events[$day][] = new Event($event);
      }
      catch ( Exception $e )
      {
         die ( $e->getMessage() );
      }
   }
   return $events;
 }
 
 /**
  * Returns HTML markup to display the calendar and events
  *
  * Using the information stored in class properties, the
  * events for the given month are loaded, the calendar is
  * generated, and the whole this is returned as valid markup.
  *
  * @return string the calendar HTML markup
  */
 public function buildCalendar()
 {
   /*
    * Determine the calendar month and create an array of
    * weekday abbreviations to label the calendar columns
    */
   $cal_month = date('F Y', strtotime($this->_useDate));
   $weekdays = array('Sun', 'Mon', 'Tues',
         'Wed', 'Thurs', 'Fri', 'Sat');
   
   /*
    * Add a header to the calendar markup
    */
   $html = "\n\t<h2>$cal_month</h2>";
   for ( $d=0, $labels=NULL; $d<7; ++$d )
   {
      $labels .= "\n\t\t<li>" . $weekdays[$d] . "</li>";
   }
   $html .= "\n\t<ul class=\"weekdays\">"
      . $labels . "\n\t</ul>";
   
   /*
    * Load events data
    */
   $events = $this->_createEventObj();
   
   /*
    * Create the calendar markup
    */
   $html .= "\n\t<ul>"; // Start a new unordered list
   for ( $i=1, $c=1, $t=date('j'), $m=date('m'), $y=date('Y');
         $c<=$this->_daysInMonth; ++$i )
   {
      /*
       * Apply a "fill" class to the boxes occurring before
       * the first of the month
       */
      $class = $i<=$this->_startDay ? "fill" : NULL;
      
      /*
       * Add a "today" class if the current date matches
       * the current date
       */
      if ( $c==$t && $m==$this->_m && $y==$this->_y )
      {
         $class = "today";
      }
      
      /*
       * Build the opening and closing list item tags
       */
      $ls = sprintf("\n\t\t<li class=\"%s\">", $class);
      $le = "\n\t\t</li>";
      
      /*
       * Add the day of the month to identify the calendar box
       */
      if ( $this->_startDay<$i && $this->_daysInMonth>=$c)
      {
         /*
          * Format events data
          */
         $event_info = NULL; // clear the variable
         if ( isset($events[$c]) )
         {
            foreach ( $events[$c] as $event )
            {
               $link = '<a href="view.php?event_id='
                     . $event->id . '">' . $event->title
                     . '</a>';
               $event_info .= "\n\t\t\t$link";
            }
         }
         
         $date = sprintf("\n\t\t\t<strong>%02d</strong>",$c++);
      }
      else { $date="&nbsp;"; }
      
      /*
       * If the current day is a Saturday, wrap to the next row
       */
      $wrap = $i!=0 && $i%7==0 ? "\n\t</ul>\n\t<ul>" : NULL;
      
      /*
       * Assemble the pieces into a finished item
       */
      $html .= $ls . $date . $event_info . $le . $wrap;
   }
   
   /*
    * Add filler to finish out the last week
    */
   while ( $i%7!=1 )
   {
      $html .= "\n\t\t<li class=\"fill\">&nbsp;</li>";
      ++$i;
   }
   
   /*
    * Close the final unordered list
    */
   $html .= "\n\t</ul>\n\n";
   
   /*
    * Return the markup for output
    */
   return $html;
 }
 
}

?>


The problem line is
Code: Select all
$stmt = $this->db->prepare($sql);


and the events class is as follows:
Code: Select all
<?php

/**
 * Stores event information
 *
 * PHP version 5
 *
 * @author      Candice Chapman
 * @copyright   2013 Holy Trinity and Saint James
 */

 class Event
{

   /**
    * The event id
    *
    * @var ing
    */
   public $id;
   
   /**
    * The event title
    *
    * @var string
    */
   public $title;
   
   /**
    * The event description
    *
    * @var string
    */
   public $description;
   
   /**
    * The event start time
    *
    * @var string
    */
   public $start;
   
   /**
    * The event end time
    *
    * @var string
    */
   public $end;
   
   /**
    * Accepts an array of event data and stores it
    *
    * @param array $event Associative array of event data
    * @return void
    */
   public function __construct($event)
   {
      if ( is_array($event) )
      {
         $this->id = $event['event_id'];
         $this->title = $event['event_title'];
         $this->description = $event['event_desc'];
         $this->start = $event['event_start'];
         $this->end = $event['event_end'];
      }
      else
      {
         throw new Exception("No event data was supplied.");
      }
   }
}

?>


Without the code to format events and call the function _createEventobj() the code works perfectly fine. I've looked at previous copies of the same file that works and I can't see any difference. I've also scoured the book and compared my code with the book and its identical. I'm completely stumped HELP!! :help:
AngelGirl88
New php-forum User
New php-forum User
 
Posts: 6
Joined: Mon Dec 10, 2012 11:23 am

Re: Fatal Error: Call to a prepare function

Postby Strider64 » Mon Jul 01, 2013 9:11 am

Check you connection DB_Connect Class and see if you are calling it correctly?

Code: Select all
class Calendar extends DB_Connect // <- This is what I'm referring to.
    {
User avatar
Strider64
New php-forum User
New php-forum User
 
Posts: 44
Joined: Sat Mar 23, 2013 8:24 am

Re: Fatal Error: Call to a prepare function

Postby johnj » Mon Jul 01, 2013 8:32 pm

Yes, the error looks like is in Db_Connect class. Post the code for that class in order to locate the error.

If the connection was properly created then $this->db would have had a proper connection resource but in this case it is not there and hence the error.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron