Mysqli insert / OO Design Problem - Call to a member function bind_param() on a non-object in...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi there,

I'm new to OO-PHP and have encountered a problem that I just can't
figure out. I have a class called DBAccess that extends mysqli. In a
nutshell, it looks like this:

class DBAccess extends mysqli {
   private static $instance;
   private static $preferences;

   /** Create an instance of DBAccess. */
   private function __construct($host, $port, $user, $pass, $name) {
       self::$instance = parent::__construct($host, $user, $pass, $name);
   }

   /** Populate and/or return an instance of DBAccess. */
   public function getInstance() {
       if (empty(self::$instance)) {
           $preferences = Preferences::getInstance();

           // Let's set the database defaults if not previously set.
           if (self::$host == null) {
                   DBAccess::setHost($preferences->getProperty("TestDbHost"));
           }
			
           if (self::$port == null) {
                   DBAccess::setPort($preferences->getProperty("TestDbPort"));
           }
   			
           if (self::$user == null) {
                   DBAccess::setUser($preferences->getProperty("TestDbUser"));
           }
   			
           if (self::$pass == null) {
                   DBAccess::setPass($preferences->getProperty("TestDbPass"));
           }
   			
           if (self::$name == null) {
                   DBAccess::setName($preferences->getProperty("TestDbName"));
           }

           self::$instance = new DBAccess(self::$host, self::$port,
               self::$user, self::$pass, self::$name);

           return self::$instance;
       }
   }

   .
   .
   .
}

I also have a class, call it MyClass, that tries to connect using
DBAccess and insert a record. It can connect to the database, but
encounter a problem (Call to a member function bind_param() on a
non-object) when I try to insert a record. Here's the MyClass class
(some of the complexity removed):

class MyClass {
   private static $conn;
   private static $preferences;

   /** Create an instance of ContactInformation. */
   public function __construct() {
       self::$conn = DBAccess::getInstance();
       self::$preferences = Preferences::getInstance();
   }

   /** Populate instance. */
   public function set($args) {
      // Calls to $this->set<ARG>;
   }

   /** Insert record. */
   public function insert() {
       $sql = "INSERT INTO table (" .
               "field_1, " .
               "field_2, " .
               "field_3) " .
               "?, " .
               "?, " .
               "?)";
       echo "Server version: " .  self::$conn->server_info;
       $stmt = self::$conn->prepare($sql);
       $stmt->bind_param('sssssssssssss',
               $this->getField1(),
               $this->getField2(),
               $this->getField3());
       $stmt->execute();
       $stmt->close();
   }
}

If I execute the following code:

$conn = DBAccess::getInstance();
$someClass = new Class();
$someClass->set(<ALL THE ARGS);
$someClass->insert();

I see the following output:

Server version: 5.0.21-Debian_3ubuntu1-log
Fatal error: Call to a member function bind_param() on a non-object in
C:\blah\blah\blah

The fact that the server version is correctly displayed, suggests that
it is connecting. Why is it having issues with the $stmt? Anyone know?

The smallprint:
The DBAccess class looks a little funky - I'm playing around with the
Singleton Design Pattern, and plan to refine it once I get the kinks
(like this) worked out.

Thanks in advance for any thoughts.

- Leee

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux