Location: PHPKode > scripts > MySQLi_Query > MySQLi_Query/MySQLi_Query.php
<?
class MySQLi_Query {
    
    private $connection;
    
    public function __construct($database, $user, $password, $host = 'localhost') {
        $this->connection = new MySQLi($host, $user, $password, $database);
    }
    
    public function __destruct() {
        $this->connection->close();
    }
    
    public function execute($query) {
        $statement = $this->connection->prepare($query);
        
        $args = func_get_args();
        $bind_params = array();
        
        //Set up the bind parameters.
        for ($i = 1; $i < count($args); $i++) {
            $bind_params[0] .= $this->getType($args[$i]);
            $bind_params[$i] = &$args[$i];
        }    
        
        print_r($bind_params[0]);
        
        if (count($bind_params) > 0) {
            call_user_func_array(array($statement, 'bind_param'), $bind_params);
        }
        
        $statement->execute();
        
        //Returns a result set, an insert id, or the number of affected rows.
        if ($resultset = $statement->get_result()) {
            $rows = array();
        } else {
            $rows = $statement->insert_id == 0 ? $statement->affected_rows : $statement->insert_id;
        }
        
        while ($resultset != null && $row = $resultset->fetch_assoc()) {
            $rows[] = $row;
        }
        
        if ($resultset != null) {
            $resultset->free();
        }
        
        $statement->close();
        
        return($rows);
    }
    
    private function getType($var) {
        $type = gettype($var);
        return($type[0]);
    }    
    
    //Helper functions.
    
    static public function toInt($var) {
        return(0 + $var);
    }
    
    static public function toDouble($var) {
        return(0.0 + $var);
    }
    
    static public function toString($var) {
        return('' . $var);
    }
}
?>
Return current item: MySQLi_Query