SitePoint Sponsor

User Tag List

Results 1 to 18 of 18

Threaded View

  1. #1
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    288
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Cannot find function "prepare" in PDO Object

    Based on an earlier thread (PDO: Am I missing the concept completely? ), I'm simply attempting to replace the EXTEND of my database manager with passing the object as an argument. Apparently whatever object I am passing does not contain the "prepare" function.

    (The other change I made from the original db was to change the class and variable name from $db to $dbManager)
    Here is the class that sets up the dbManager object
    PHP Code:
    <?php
    Class lessons_template_dbManager 
      Protected 
    $dbManager null;
        Protected 
    $trace false;

     Public Function 
    __Construct() { // assure connection
        
    if($this->trace) {
            echo(
    "<br />lessons_template_dbmanager->Construct");
        }
        if(isset(
    $this->dbManager)) {
            if(
    $this->trace) {
                echo(
    "<br />lessons_template_dbmanager->db is set");
            }
            return 
    true;
        }
        if(
    $this->trace) {
            echo(
    "<br />lessons_template_db->dbmanager is NOT set");
        }

        
    $currentHost pathinfo($_SERVER['REMOTE_ADDR'],PATHINFO_BASENAME);
        if(
    $currentHost == "127.0.0.1"){        // no place like home
            
    $theUsername "root";
            
    $thePassword "";
        } else {
    --> 
    detail omitted <--
        }
      try {
          
    $this->dbManager = new PDO('mysql:host=localhost;dbname=lessons;charset=UTF-8'$theUsername$thePassword);
          
    $this->dbManager->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
          
    $this->dbManager->setAttribute(PDO::ATTR_EMULATE_PREPARESfalse);
      }
      catch(
    PDOException $ex) {
            echo(
    "<p style='font-weight: bold; color: red; font-size: larger;'><br /><br /><br />A serious error occurred, contact programmer who will need the following detail<br /></p>");
            echo(
    "<br />lessons_template_dbManager->Construct<br />");
        
    var_dump($ex);
        exit();
      }
     } 
    // end function
    // end class 
    ?>
    Here's an excerpt class that uses the above passed as an argument
    PHP Code:
    <?php
    Class lessons_horse_dbManager {

        protected 
    $trace true;
        private 
    $dbManager;

        Public Function 
    __Construct($dbManager) {
            if(
    $this->trace) {
                echo(
    "<br />lessons_horse_dbManager->__Construct");
            }
            
    $this->dbManager $dbManager;
        }
        Public Function 
    GetHorseList() {
            if(
    $this->trace) {
                echo(
    "<br />lessons_horse_dbManager->GetHorseList");
            }
            
    // dispensing with try-catch
            
    $stmt $this->dbManager->prepare("SELECT name FROM horses ORDER BY name");
            
    $stmt->execute();
            
    $nameList = array();
            while(
    $row $stmt->fetch(PDO::FETCH_ASSOC)) {
                
    $nameList[] = $row['name'];
                } 
    // end while
            
    return($nameList);
            } 
    // end function  
    ...
    And here's the calling sequence from the instantiating class
    PHP Code:
    Class lessons_horse_Form
     
    extends lessons_template_Base{
    ...
        Function 
    BuildContent(){
    ...
            
    $dbManager = new lessons_template_dbManager();
            
    $horseObj = new lessons_horse_dbManager($dbManager);
            
    $horseValues $horseObj->GetHorseList(); 
    The message I get is
    Fatal error: Call to undefined method lessons_template_dbManager::prepare() in C:\xampp\htdocs\lessons\horse\dbmanager.php on line 18
    [I have "no icon" selected, the message should show as ...dbManager colon colon prepare()]

    Where line 18 is '$stmt = $this->dbManager->prepare("SELECT name FROM horses ORDER BY name");' from class lessons_horse_dbManager. (And again, this worked when I extended the original lessons_template_db class).



    Any ideas?
    Regards,
    grNadpa
    Last edited by SpacePhoenix; Oct 21, 2012 at 12:50. Reason: swapped code tags for php tags around php code


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •