Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: more classes..

  1. #1
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    more classes..

    hey

    heres the deal- im making a website that has drink recipes on it, and it uses a class to connect to the db, so i figured why not use a class to grab and modify drinks

    so, for starters, heres my db class:
    Code:
    class Database{
      
      // private instant variables
      var $dbConnectionID;
      var $queryID;
      var $record; 
      var $host;
      var $database;
      var $user;
      var $password;
      
      /* 
        constructor
        connect to datbase server and select specified database
      */
      function Database($host=MYSQL_HOST, $db=MYSQL_MAIN_DB, $user=MYSQL_USER, $pwd=MYSQL_PASS){
        $this->host = $host;
        $this->database = $db;
        $this->user = $user;
        $this->password = $pwd;
        $this->connect();
      }
      
      
      /*
        private method
        used internally to generate dbConnectionID
      */
      function connect(){
        $this->dbConnectionID = @mysql_pconnect($this->host, $this->user, $this->password);
        if(!$this->dbConnectionID){
          echo(mysql_errno().":".mysql_error());
          exit;
        }
        else{
          $status = @mysql_select_db($this->database, $this->dbConnectionID);
          if(!$status){
            echo(mysql_errno().":".mysql_error());
            exit;
          }
        } 
      }
      
      
      //  public methods
      
      function query($sql){
        // connect to db incase connection id is not set
        if(empty($this->dbConnectionID))
          $this->connect();
        $this->queryID = @mysql_query($sql, $this->dbConnectionID);
        
        // handle error
        if(!$this->queryID){
          echo(mysql_errno().":".mysql_error());
          exit;
        } else {
    		$this->nextRecord();	// had to add this in so you dont have to manually skip to the first record, (1 not 0) -jr
    	}
      }  
      
      function nextRecord(){
        $this->record = @mysql_fetch_array($this->queryID);
        $status = is_array($this->record);
        return($status);
      }
      
      function numRows(){
        $rows = @mysql_num_rows($this->queryID);
        return($rows);
      }
      
      // get record field value from the current record pointed by $record
      function getField($field){
        return($this->record[$field]);
      }
    }
    ...and heres my drink class...

    Code:
    class drink {
    
    	var $id;
    	var $title;
    	var $ingredients;
    	var $directions;
    	var $user;
    	var $submitted;
    	var $rating;
    	var $ratings;
    	var $views;
    	var $rating_addrs;
    	var $online;
    	
    	function select($tmp_id) {
    		global $db;
    		$this->id = $tmp_id;
    		unset($tmp_id);
    		if ($db->query("SELECT * FROM tt_drinks WHERE id = ".$this->id)) {
    			return true;
    			$this->id = $db->getField("id");
    			$this->title = $db->getField("title");
    			$this->ingredients = $db->getField("ingredients");
    			$this->directions = $db->getField("directions");
    			$this->user = $db->getField("user");
    			$this->submitted = $db->getField("submitted");
    			$this->rating = $db->getField("rating");
    			$this->ratings = $db->getField("ratings");
    			$this->views = $db->getField("views");
    			$this->rating_addrs = $db->getField("rating_addrs");
    			$this->online = $db->getField("online");
    		} else {
    			// debug
    			echo mysql_error();
    			echo "oops";
    			return false;
    		}
    	}
    	
    	function delete($id) {
    		global $db;
    		if (@$db->query("DELETE FROM tt_drinks WHERE id = ".$id)) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    	
    	function add() {
    		global $db;
    		if (@$db->query("INSERT INTO tt_drinks VALUES(".
    				$this->id.",".
    				$this->title."','".
    				$this->ingredients."','".
    				$this->directions."','".
    				$this->user."','".
    				$this->submitted."','".
    				$this->rating."','".
    				$this->ratings."','".
    				$this->views."','".
    				$this->rating_addrs."','".
    				$this->online."')")) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    
    	function modify() {
    		global $db;
    		// delete the old entry
    		$this->delete($this->id);
    		// add the new one, with old ID
    		if ($this->add()) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    
    }
    by looking at the code you can pretty much guess how it works

    and heres my file thats supposed to show some output:

    Code:
    // start our default (main) database
    require("inc/database.class.php");		// database connector class
    $db = new Database;
    
    // includes - seperate files for inclusion, classes, etc
    require("inc/drink.class.php");			// drink operations class
    
    $drink = new drink;
    $drink->select(453);
    when i run that code itoutpust my error message "oops"

    ive tried running that individual query alone $db->query("SELECT * FROM tt_drinks WHERE id = ".$this->id) and it runs fine!

    any ideas why this may not be working (ps its no a mysql syntax error cuz mysql_error() doesnt output anything

    thanks in advance!
    -john

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,093
    Thanks
    11
    Thanked 101 Times in 99 Posts
    Hi , your DB class returns nothing so $db is not a resource...
    add
    PHP Code:
    <?
        
    ......
        
    $this->connect();
        return 
    $this->dbConnectionID;
    }
    ?>
    to the Database constructor
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •