views:

22

answers:

0

Hi guys i have a theory question to do with PHP and creating an API... The API i am planning on creating will sit in front of a database and will be used to validate data and execute actions within a pre-set method avoiding people amending data directly..

Now my question is what is the best method to structure the API..

Is there a need to separate database queries from the main function logic? My initial thought was to go three levels deep..

and secondly what is the correct way to error handle with multiple level function calls.

The add_reseller function called a second function to check if that reseller exists.. If that function returns false then it is assumed that it doesn't and moves on to insert the record via another function. Now if the lookup function fails ie. errors out, the same result exists..

So i am wondering what the "correct" method for this would be in the real world.. How would one go about structuring an API / class and error handling correctly. Are stand along mysql functions required?

Thanks

<?php

require_once('auth.base.class.php');

class auth_api extends bases
{
   public function __construct()
   {
      if ( !$this->db_connect() ) die("Could not connect to API database server");
   }

   public function reseller_add($args="") 
   {
      if ( empty($args) || !isset($args['name']) || empty($args['name']) )
      {
         $this->error[] = 'name attribute is required';
         return false;
      }

      // Insert the reseller
      if ( in_reseller($args['name']) ) return false;

      return true;
   }

   public function in_reseller($name='')
   {
      $sql = "INSERT INTO reseller ('name') VALUES ('$name')";
   }
?>

<?php

abstract class base
{  
   /* 
   * Error Functions 
   */

   protected $error;

   public function get_last_error()
   {
      return end($this->error);
   }

   public function get_all_error() 
   {
      return($this->error);
   }

   /* 
   * Database functions 
   */

   private $db_link;
   private $db_user     = "x";
   private $db_pass     = "x";
   private $db_host     = "x";
   private $db_database = "x";
   private $db_result;

   public function db_connect()
   {
      $this->db_link = mysql_connect( $this->db_host, 
                                      $this->db_user,
                                      $this->db_pass );
      if ( !$this->db_link ) 
      {
         $this->error[] = 'could not connect: ' . mysql_error();
         return false;
      }

      // Connect to database
      if ( !mysql_select_db($this->db_database, $this->db_link) )
      {
         $this->error[] = 'could not select database: ' . mysql_error();
         return false;
      }

      return true;
   }

   public function db_disconnect()
   {
      mysql_close($this->db_link);
      return true;
   }

   public function db_query($sql='')
   {
      if ( empty($sql) )
      {
         $this->error[] = "query string missing";
         return false;
      }

      $this->result = mysql_query($sql, $this->link);
   }

   public function db_num_row()
   {
      return mysql_num_rows($this->db_result);
   }

   public function db_affected_row()
   {
      return mysql_affected_rows($this->link);
   }

   public function db_fetch_row()
   {
      return mysql_fetch_assoc($this->result)
   }

   public function db_fetch_array()
   {
      $result = array(); 
      while ($row = mysql_fetch_assoc($this->result) )
      {
         $result[] = $row;
      }
      return $result;
   }
}

?>