Use of PDO in classes

后端 未结 4 994
面向向阳花
面向向阳花 2020-11-28 01:40

I have a few classes that perform some MySQL queries and prepared statements. However, I am lost in how to incorporate my PDO object within those classes. For example, I w

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 02:11

    Here is a mostly complete working cut & paste example of Guillaume Boschini's answer above.

    A populated DB table (MySQL):

    CREATE TABLE `useraddress` (                                                                                                                                        
      `addressid` int(10) unsigned NOT NULL AUTO_INCREMENT,                                                                                                                             
      `userid` int(10) unsigned NOT NULL,                                                                                                                                               
      `addresstitle` char(100) NOT NULL,                                                                                                                        
      `streetaddressa` char(100) NOT NULL,
      `streetaddressb` char(100) DEFAULT NULL,
      `unit` char(50) DEFAULT NULL,
      `city` char(50) NOT NULL,
      `state` char(2) NOT NULL,
      `zip` int(5) NOT NULL,
      `zipplusfour` int(4) DEFAULT NULL,
      PRIMARY KEY (`addressid`),
      KEY `userid` (`userid`),
      CONSTRAINT `useraddress_fk_1` FOREIGN KEY (`userid`) REFERENCES `user` (`userid`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
    

    In /DBLibrary/pdocore.php:

    dbh = new PDO($dsn, $user, $password);
                $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
            }
    
            public static function getInstance() {
                if (!isset(self::$instance)) {
                    $object = __CLASS__;
                    self::$instance = new $object;
                }
                return self::$instance;
            }
    
            // others global functions
        }
    ?>
    

    In /objectsLibrary/SYS_UserAddress.php:

    dbh->prepare($query);
    
                    $queryArray = array(':addressid'=>$_addressid);
    
                    if ($pdoObject->execute($queryArray)) {
    
                        $pdoObject->setFetchMode(PDO::FETCH_ASSOC);;
    
                        while ($addressrow = $pdoObject->fetch()) {
    
                            $this->addressid=$addressrow[k_uaddress_addressid];
                            $this->userid=$addressrow[k_uaddress_userid];
                            $this->addresstitle=$addressrow[k_uaddress_addresstitle];
                            $this->addressa=$addressrow[k_uaddress_addressa];
                            $this->addressb=$addressrow[k_uaddress_addressb];
                            $this->unit=$addressrow[k_uaddress_unit];
                            $this->city=$addressrow[k_uaddress_city];
                            $this->zip=$addressrow[k_uaddress_zip];
                            $this->zipplusfour=$addressrow[k_uaddress_zipplusfour];
    
                        }
                        $returnValue=TRUE;
                    }
                }
                catch(PDOException $pe) {
                    trigger_error('Could not connect to MySQL database. ' . $pe->getMessage() , E_USER_ERROR);
                }
    
                return $returnValue;
    
            }
        }
    
        $test=1;
        $testAddressId=2;
    
        if($test>0) {
    
            $testAddress = new SYS_UserAddress();
    
            $testAddress->SYS_UserAddressByAddressId($testAddressId);
    
            echo '
    ';
            echo print_r($testAddress);
            echo '
    '; } ?>

    The post above really helped me. This post I am making now would have gotten me to where I wanted to be faster. That is all. If anything isn't right, I'll be around to fix it.

提交回复
热议问题