Jump to content

Code Review Request


PHPwannaB

Recommended Posts

Hi All,

 

I'm trying to brush up on my back-end development skills and wrote these small classes to deal with modifying and retrieving data from a mySQL database. Would anyone mind taking the time to look over this code and let me know if I'm following sane programming practices or if need to completely rethink my approach?

 

File: DB.php

<?php
    include("connection-vals.inc.php");
    
    class DB {          
          public function validateUserInput($input){
            $this->connect();
            
            if(is_array($input)) {
                $inputString = mysql_escape_string(implode("#.!.#", $input));
                $input =  explode("#.!.#", $inputString);                
            } else {
                $input = mysql_escape_string($input);
            }
            
            $this->close();
            
            return $input;
        }
                
        protected $connectionID = False;        
        
        protected function connect(){
            $this->connectionID = mysql_connect(
                $this->host,
                $this->user,
                $this->pw
            ) or die($this->showError(mysql_error()));
            
            mysql_select_db(SELECTED_DB, $this->connectionID)
            or die($this->showError(mysql_error()));
        }       
        
        protected function close(){
            mysql_close($this->connectionID);
        }              
        
        protected function showError ($msg){
            echo "<p><strong>DB Error: </strong>$msg</p>";
        }   
        
        private $host = HOST;
        private $user = USER;
        private $pw = PASSWORD;             
    }
?>

 

File: DBFetcher.php

<?php
    include("db.php");
    
    class DBFetcher extends DB{
        public function fetchDataAsXML($query){
            $this->connect();            
            $response = mysql_query($query, $this->connectionID);            
            $this->close();
            
            if($response) {
                header("Content-type: text/xml");
                return $this->convertResponseToXML($response);
            } else {
                header("Content-type: text/html");
                $this->showError("No Response from the DB. Is your query well-formed?");
                return false;
            } 
        }
        
        private function convertResponseToXML($response){            
            $result = "";
            $result = $result . "<response>";
            
            while($row = mysql_fetch_assoc($response)){
                $result = $result . "<row>";
                foreach($row as $key => $value){
                    $result = $result . "<" . $key . ">" . $value . "</" . $key . ">";
                }
                $result = $result . "</row>";
            }
            
            $result = $result . "</response>";
            return $result;
        }
    }
?>

 

File: DBModifier.php

<?php
    include("db.php");

    class DBModifier extends DB{                
        public function modify($query){
            $this->connect();
            
            $response = mysql_query($query, $this->connectionID);
            
            $this->close();
            
            if($response) {
                return true;
            } else {
                $this->showError("Unable to update the DB. Is your query well-formed?");
                return false;
            }            
        }
    }
?>

Any help at all would be greatly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/118018-code-review-request/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.