Search Webmaster Help and Solution

Using PDO in another class

How can I use the PDO class in another class ?

Example I want to use PDO in

PHP Code: class shirt {
    var $id;
    var $filename;
    var $template;
    var $side = front;
    var $category = shirt;
    var $fonts = array();
    var $images = array();
    
    function shirt() {
        $this->CreateID();
    }
    
    function CreateID() {
        $length = 5;
        
        if (!isset($_SESSION[id])) { //IF SESSION NOT SET, CREATE RANDOM ID
            $aZ09 = array_merge(range(A, Z), range(a, z),range(0, 9));
            $rand =;
            for($c=0; $c < $length; $c++) {
               $rand .= $aZ09[mt_rand(0,count($aZ09)-1)];
            }
            
            $this->id = strtolower($rand . time());
            $this->PrepareTemplate();
            
        }
        else {
            $this->id = $_SESSION[id];
        }
    }
    
    function PrepareTemplate() {            
        HERE IT GOES WRONG
        $sql = $db->prepare(SELECT a.id, a.front, a.back, b.title FROM items a, categories b WHERE a.categoryid=b.id LIMIT 1 ASC);
        $sql->execute();    
        $preview = $sql->fetchAll();
        
        $db = null;
        
        print_r($preview);    
    }
}
I have no idea where to create the PDO $db ? How can I use the PDO class in another class ?

Example I want to use PDO in

PHP Code: class shirt {
    var $id;
    var $filename;
    var $template;
    var $side = front;
    var $category = shirt;
    var $fonts = array();
    var $images = array();
    
    function shirt() {
        $this->CreateID();
    }
    
    function CreateID() {
        $length = 5;
        
        if (!isset($_SESSION[id])) { //IF SESSION NOT SET, CREATE RANDOM ID
            $aZ09 = array_merge(range(A, Z), range(a, z),range(0, 9));
            $rand =;
            for($c=0; $c < $length; $c++) {
               $rand .= $aZ09[mt_rand(0,count($aZ09)-1)];
            }
            
            $this->id = strtolower($rand . time());
            $this->PrepareTemplate();
            
        }
        else {
            $this->id = $_SESSION[id];
        }
    }
    
    function PrepareTemplate() {            
        HERE IT GOES WRONG
        $sql = $db->prepare(SELECT a.id, a.front, a.back, b.title FROM items a, categories b WHERE a.categoryid=b.id LIMIT 1 ASC);
        $sql->execute();    
        $preview = $sql->fetchAll();
        
        $db = null;
        
        print_r($preview);    
    }
}
I have no idea where to create the PDO $db ?

View Complete Thread with Replies

Related Items

Query failed: connection to localhost:3354 failed (errno=111, msg=Connection refused).