PHP/MySQL Help With OOP

Kipkip

Join the Revolution
  • 967
    Posts
    21
    Years
    • Age 34
    • Seen Jun 24, 2007
    PHP:
    <?php
    class MySQL
    {
      var $row;
      function add_cat($name)
    	{
    	  $insert = "INSERT INTO Catergories(Name) VALUES ($this->name)";
    		if(mysql_query($this->insert)){
    		  return true;
    	  }else{
    		  return false;
    		}
    	}
    	function show_cat()
    	{
    	  $query = mysql_query("SELECT ID, Name FROM Catergories ORDER BY Name");
        while ($this->row = mysql_fetch_array($this->query) or die(mysql_error())){
          $id = $this->row['id'];
          $name = $this->row['name'];
          print("<a href=\"?delete=$this->id\">$this->name</a><br />");
        }
    	}
    	function delete_cat( $id )
    	{
    	  $query = "DELETE FROM Catergories WHERE ID = $this->id";
    		if(mysql_query($this->query)){
    		  return true;
    		}else{
    		  return false;
    		}
    	}
    }
    ?>
    In the add_cat() function, it won't submit the thing and returns false, getting one of my errors. And the show_cat() function shows this error:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/kipkip/public_html/news/admin/function_list_cat.php on line 17
    Help. I'm just learning OOP.
     
    let me see if i understand what you are doing

    PHP:
          $insert = "INSERT INTO Catergories(Name) VALUES ($this->name)";
            if(mysql_query($this->insert)){
              return true ();
          }else{
              return false ();
            }

    try working with that
     
    Remember, anytime you use $this, it refers to variables or functions that are created within that class only. So, to start, at the beginning of your script, you'll also want to declare
    PHP:
    var $insert;
    var $query;
    Now, for your function, anytime you're wanting to work with a variable from inside the class, you always have to prefix it with $this->. However, if you're inside of a function and you're using one of the arguments provided to the function (as in: add_cat($name), you would refer to the variable $name WITHOUT the $this-> prefix). So, rewriting the first function:
    PHP:
      function add_cat($name) 
      { 
          $this->insert = "INSERT INTO Catergories(Name) VALUES ($name)"; 
          if(mysql_query($this->insert))
          { 
              return true; 
          }
          else
          { 
              return false; 
          } 
      }

    See if that helps out.
    Cheers, John
     
    Last edited:
    No, that still doesn't help, JDS. I'm probally going to figure out this error using dumb luck like the last time. :D
     
    Well, that's just to fix that one function - you have other things in your class missing, such as a valid MySQL connection.

    Here, I'll post the class I made for PPN's pokedex and let you look over it.
    PHP:
    /* Class File */
    
    //////////////////////////////////////////////////////
    //
    //		Dataase Connection Class
    //			Copyright 2004 John Slone
    //
    //
    //////////////////////////////////////////////////////
    
    
    /* Database class */
    // Handles all interaction with database
    
    class DB {
    	
    	var $username = 'root';
    	var $password = '';
    	var $database = 'JWAC';
    	var $host = 'localhost';
    	var $result = array();
    	
    	var $connect_id = 0;
    	var $query_id = 0;
    	
    	var $querycount = 0;
    	var $tempquerycount = 0;
    	
    	// Beginning of functions
    	
    	// Connect function
    	// -connects to database
    	
    	function connect() {
    		
    		if (0 == $this->connect_id) {
    			// No connection present.
    			// Do we have a password?
    			if (!$this->password) {
    				$this->connect_id = mysql_connect($this->host,$this->username);
    			} else {
    				$this->connect_id = mysql_connect($this->host,$this->username,$this->password);
    			}
    			
    		}
    		
    		if (!$this->connect_id) {
    			exit("<b>Fatal Error:</b> Unable to connect to the database using those settings.");
    		} else {
    			if (!mysql_select_db($this->database,$this->connect_id)) {
    				exit("<b>Fatal Error:</b> Unable to select $this->database for viewing.");
    			}
    		}
    	}
    	
    	// Query function
    	// performs an SQL query
    	
    	function query($queryline) {
    		global $querycount;
    		if (!$queryline) {
    			exit("You must specify a query to execute.");
    		} else {
    			$this->query_id = @mysql_query($queryline, $this->connect_id);
    // 			if (!$this->query_id) {
    // 				die ("<i>$queryline</i> did not execute.<br />".mysql_error());
    // 			}
    			$this->querycount++;
    		}
    		if (!$this->query_id) {
    			return false;
    		}
    		return $this->query_id;
    	}
    	
    	function fetch($query_id) {
    		if ($query_id) {
    			$this->query_id = $query_id;
    		}
    		return @mysql_fetch_array($this->query_id);
    	}
    	
    	// Num rows function
    	// tells the number of rows in a result set.
    	
    	function numrows($query_id) {
    		if ($query_id) {
    			$this->query_id = $query_id;
    		}
    		return @mysql_num_rows($this->query_id);
    	}
    	
    	// insert_id function
    	// gets the autokey entry for the last inserted() row
    	
    	function insert_id() {
    		return @mysql_insert_id($this->connect_id);
    	}
    	
    }

    See if that helps.
     
    Ok. Here's the whole file now:
    PHP:
    <?php
    class MySQL
    {
        //////////
        //Database Connection Variables
    	//////////
    	var $dbname = "news";
    	var $dbuser = "root";
    	var $dbpass = "<PASSWORD>";
    	var $dbhost = "localhost";
    	//////////
    	//Other Variables
    	//////////
    	var $row;
    	var $insert;
        var $query = "mysql_query(\"SELECT ID, name FROM Catergories ORDER BY Name\")";
    	var $delete;
    	var $id;
    	var $name;
    	//////////
    	//Functions
    	//////////
    	function connection()
    	  {
    		  mysql_connect ($this->dbhost, $this->dbuser, $this->dbpass) or die ('I cannot connect to the database because: ' . mysql_error());
          mysql_select_db ($this->dbname);
    		}
      function add_cat($name)
        {
          $this->insert = "INSERT INTO Catergories(Name) VALUES ($name)";
          if(mysql_query($this->insert)){
            return true;
          }else{
    			  return false;
          }
        } 
    	function show_cat()
    	{
        while ($this->row = mysql_fetch_array($this->query) or die){
          $this->id = $this->row['ID'];
          $this->name = $this->row['name'];
          print("<a href=\"?delete=$this->id\">$this->name</a><br />");
        }
    	}
    	function delete_cat($id)
    	{ 
    	  $this->delete = mysql_query("DELETE FROM Catergories WHERE ID = $id");
    		if($this->delete){
    		  return true;
    		}else{
    		  return false;
    		}
    	}
    }
    ?>
    Still won't work. :\
     
    And how are you calling the database class in your script?
     
    JDS said:
    And how are you calling the database class in your script?
    Like this:
    PHP:
    $new = new MySQL;
    $new->connection();
    EDIT: Apparently, there was something wrong with the database I was using. I swithed to another database and evrything is now working fine. I'm going to look into what went wrong with the database. Thanks for you help, JDS.
     
    Last edited:
    Back
    Top