Jump to content
Fórum Script Brasil
  • 0

Classe para manipular bancos mysql


vini_loock

Question

Ainda falta finalizar o método de antinjecton(o validate()), mas ainda só usei ela em painéis administrativos, onde não há necessidade disso.

<?php
    /*----------------------------------------------------------------------------------------------------//
    //---------------------Class para manipulação de banco de dados - PHP---------------------------------//
    //----------------------------------------------------------------------------------------------------//
    //-----Todos os direitos reservados - Vinicius Siuta Borges-------------------------------------------//
    //-----Contato: viniciussiutaborges@hotmail.com-------------------------------------------------------//
    //--------||--: viniciussiutaborges@gmail.com---------------------------------------------------------//
    //----------------------------------------------------------------------------------------------------*/
    class Database{
        private $hostname = 'localhost', $username = 'root', $password = '', $database, $connect;
        
        public function setHostname($host){
            $this->hostname = $host;
            return $this;
        }
        
        public function setUsername($user){
            $this->username = $user;
            return $this;
        }
        
        public function setPassword($pass){
            $this->password = $pass;
            return $this;
        }
        
        public function setDatabase($db){
            $this->database = $db;
            return $this;
        }
        
        public function getHostname(){
            return $this->hostname;
        }
        
        public function getUsername(){
            return $this->username;
        }
        
        public function getPassword(){
            return $this->password;
        }
        public function getDatabase(){
            return $this->database;
        }
        
        public function validate($txt){
            return $txt;
        }
        
        public function connect(){
            $this->connect = mysql_connect($this->hostname, $this->username, $this->password);
            mysql_select_db($this->database, $this->connect);
            return $this;
        }
        
        public function query($query, $arr = array('')){
            for($i = 0; $i < count($arr); $i++){
                $query = str_replace('$'.$i, $this->validate($arr[$i]), $query);
            }
            $query = mysql_query($query);
            if(!$query)return false;
            return $query;
        }
        
        public function rows($query){
            if(!$query)return false;
            return mysql_num_rows($query);
        }
        
        public function fetch($query){
            $result = '';
            if($this->rows($query) > 0){
                $i = 0;
                while($var = mysql_fetch_array($query)){
                    foreach($var as $chave => $value){
                        $result[$i][$chave] = $value;
                    }
                    $i++;
                }
            }
            return $result;
        }
        
        public function disconnect(){
            mysql_close($this->connect);
            return $this;
        }
    }
?>

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      651.8k
×
×
  • Create New...