[MySQLi] prepared statements class

Results 1 to 3 of 3
  1. #1
    Lurking around Clawed is offline
    MemberRank
    Jun 2012 Join Date
    RaGEZONELocation
    785Posts

    [MySQLi] prepared statements class

    Wasn't going to show this off, but I started to wonder if I could improve it.
    It works like a charm, just wanted to know what other people thought.

    PHP Code:
    class DatabasePreparedStatement
    {
        private 
    $connection$statement$numrows$fetch$insertid$insert false;
        public 
    $results = array();
        
        public function 
    SetConnection($connection)
        {
            
    $this->connection $connection;
            
            return 
    $this;
        }
        
        public function 
    Prepare($query)
        {
            if(!
    $this->statement $this->connection->prepare($query))
            {
                
    $this->__Error($this->connection->error);
            }
            
            return 
    $this;
        }
        
        public function 
    Bind($data)
        {
            
    $types "";
            
            foreach(
    $data as $key => $value)
            {
                
    $types .= $this->__Type($value);
            }
            
            
    $params = array($types);
            
            foreach(
    $data as $key => $value)
            {
                
    $params[] =& $data[$key];
            }
            
            
    call_user_func_array(array($this->statement"bind_param"), $params);
            
            return 
    $this;
        }
        
        public function 
    Results($data)
        {
            
    $results = array();
            
            foreach(
    $data as $key)
            {
                
    $results[$key] = "";
            }
            
            
    call_user_func_array(array($this->statement"bind_result"), &$results);
            
    $this->results $results;
            
            return 
    $this;
        }
        
        public function 
    Execute()
        {
            if(!
    $this->statement->execute())
            {
                
    $this->__Error($this->statement->error);
            }
            
            return 
    $this;
        }
        
        public function 
    Store()
        {
            if(!
    $this->statement->store_result())
            {
                
    $this->__Error($this->statement->error);
            }
            
            return 
    $this;
        }
        
        public function 
    NumRows()
        {
            
    $this->numrows $this->statement->num_rows;
            
            return 
    $this;
        }
        
        public function 
    Fetch($while false)
        {
            if(
    $while != true)
            {
                
    $this->statement->fetch();
                
                return 
    $this;
            }
            else
            {
                return 
    $this->statement->fetch();
            }
        }
        
        public function 
    InsertId($insert false)
        {
            
    $this->insert $insert;
            
    $this->insertid $this->statement->insert_id;
            
            return 
    $this;
        }
        
        public function 
    Close()
        {
            if(!
    $this->statement->close())
            {
                
    $this->__Error($this->statement->error);
            }
            
            return 
    $this;
        }
        
        private function 
    __Error($string)
        {
            
    $error = new CoreError;
            die(
    $error->SetError($string)->Call());
        }
        
        private function 
    __Type($var)
        {
            return 
    substr(gettype($var), 01);
        }
        
        public function 
    Call()
        {
            if(
    $this->insert != false)
            {
                return 
    $this->insertid;
            }
            elseif(
    $this->results)
            {
                return 
    $this->results;
            }
            elseif(
    $this->numrows 0)
            {
                return 
    $this->numrows;
            }
            elseif(
    $this->numrows 1)
            {
                return 
    0;
            }
            else
            {
                return 
    false;
            }
        }



  2. #2
    hi academic is offline
    MemberRank
    Jun 2010 Join Date
    AustraliaLocation
    484Posts

    Re: [MySQLi] prepared statements class

    Is their a CoreError class? If so, you could make the SetError method static so you don't have to init it from within the class.

  3. #3
    Novice ryelson is offline
    MemberRank
    Sep 2012 Join Date
    2Posts

    Re: [MySQLi] prepared statements class

    Quote Originally Posted by ACADEMiC View Post
    Is their a CoreError class? If so, you could make the SetError method static so you don't have to init it from within the class.


    Yeah. i agree.



Advertisement