Location: PHPKode > scripts > CMySQL > cmysql/cmysql-beta.php
<?php
	/** 
	 * Fast MySQL functions class 
	 *
	 * Author: 
	 * 	Fernando Gabrieli, FGDE, fgde.com.ar
	 *
	 * Contributors:
	 * 	Uriel Sepe, FGDE, fgde.com.ar
	 * 	Gabriel Caraballo, FGDE, fgde.com.ar
	 * 	Fernando Gabrieli, FGDE, fgde.com.ar
	 *
	 * Public functions:
	 *
	 * 	- a() / ai() / a_id() -> add a new entry
	 *
	 * 	- m() -> edit an entry 
	 *
	 * 	- r_by_id() -> remove an entry by id 
	 *
	 *
	 * Pass an array, functions will make it fit!
	 *
	 * Objective of this class: make an user-friendly class to interact
	 * with MySQL databases for PHP (may be extended to other languages
	 * if somebody thinks it is useful)
 	 *
	 */ 

	class cmysql
	{
		private $db = 'db_name';

    /**
		 * order function		
		 *
		 * Description:      
		 *    order the array with the data, uses the php mysql functions.		 
     *    
     * Params:
     *    $t - the table.
     *    $x - an array with the values.
     *    
     * Returns:
     *    the generated array with the content in order.
     *
     */
		private function order($t, $x)
		{
			$r = mysql_list_fields($this->db, $t);

			for ($i = 0 ; $i < mysql_num_fields($r) ; $i++)
			{
				$n = mysql_field_name($r, $i);

				$f[$i]["name"] = $n;
				$f[$i]["value"] = $x[$n];
				$f[$i]["type"] = mysql_field_type($r, $i);
			}

			return( $f);
		}

    /**
		 * format_value function		
		 *
		 * Description:      
		 *    parse a string to detect and replace the use of quotes or certain 
		 *    characters (add more if its necesary to apply another format).
     *    <Quote use for mysql>
     *
     * Params:
     *    $value - the string to be parsed.
     *    
     * Returns:
     *    the parsed string.
     *
     */
		private function format_value($value)
		{
			$value = str_replace("\"", "\\\\\"", $value);
 			$value = preg_replace("/('+)/", "'", $value);

			return( $value);
		}

		/**
		 * sql function		
		 *
		 * Description:      
		 *    execs a mysql query.
     *
     * Params:
     *    $q - the query.
     *    
     * Returns:
     *    the result set generated (all the arrays, from mysql_fetch_array).
     *
     */
		public static function sql($q)
		{
			$r = mysql_query($q);

			if (!$r || mysql_num_rows($r) == 0)
				return( -1);

			for ($i = 0 ; $i < mysql_num_rows($r) ; $i++)
			{
				$rows[$i] = mysql_fetch_array($r);
			}

			return( $rows);
		}

    /**
		 * get_entries function		
		 *
		 * Description:      
		 *    Get all the entries from a table.
     *
     * Params:
     *    $t - the table.
     *    
     * Returns:
     *    all the rows from a specific table.
     *
     */
		public function get_entries($t) 
		{
			$q = "select * from $t";
			$r = mysql_query($q);

			for ($i = 0 ; $i < mysql_num_rows($r) ; $i++)
			{
				$rows[$i] = mysql_fetch_array($r);
			}

			return( $rows);
		}

    /**
		 * get_entries_az function
		 *
		 * Description:      
		 *    Get all the entries from a table order by a specific field from a to z.
     *
     * Params:
     *    $t - the table.
     *    $field - the field (to make the order by).
     *         
     * Returns:
     *    the result set generated by the query.
     *
     */
		public function get_entries_az($t, $field) 
		{
			$q = "select * from $t order by $field desc";
			$r = mysql_query($q);

			for ($i = 0 ; $i < mysql_num_rows($r) ; $i++)
			{
				$rows[$i] = mysql_fetch_array($r);
			}

			return( $rows);
		}

    /**
		 * get_entries_by_date function
		 *
		 * Description:      
		 *    Get all the entries from a table ordered by date.
     *
     * Params:
     *    $t - the table.
     *         
     * Returns:
     *    the result set generated by the query.
     *
     */
		public function get_entries_by_date($t) 
		{
			$q = "select * from $t order by date desc";
			$r = mysql_query($q);

			for ($i = 0 ; $i < mysql_num_rows($r) ; $i++)
			{
				$rows[$i] = mysql_fetch_array($r);
			}

			return( $rows);
		}
    
    /**
		 * get_by_id function
		 *
		 * Description:      
		 *    Get the entry from the table $t, that match with the string id 
		 *    specified in the $id param.
     *
     * Params:
     *    $t - the table.
     *    $id - the string id.
     *     
     * Returns:
     *    on success the entry that match with the id, otherwise returns zero.
     *
     */
		public function get_by_id($t, $id)
		{
			$q = "select * from $t where id=\"$id\"";

			return( cmysql::sql($q));
		}
		
    /**
		 * get_by_id2 function
		 *
		 * Description:      
		 *    Get the entry from the table $t, that match with the numeric id 
		 *    specified in the $id param.
     *
     * Params:
     *    $t - the table.
     *    $id - the numeric id.
     *     
     * Returns:
     *    on success the entry that match with the id, otherwise returns zero.
     *
     */
		public function get_by_id2($t, $id)
		{
			$q = "select * from $t where id=$id";

			return( cmysql::sql($q));
		}

    /**
		 * get_by_id2 function
		 *
		 * Description:      
		 *    Get all the entries from the table $t, that matches with the content
		 *    of $value in the field $field.		 
     *
     * Params:
     *    $t - the table.
     *    $field - the searched field.
     *    $value - the value of the field.
     *    $type - the type of the field.
     * 
     * NOTES:     
     * 
     * The $type param:
     *    A string that contains the name of the type of a field.
     *    You can easily get it with the mysql_field_type() function (from the
     *    mysql API for php, included in php).
     *                     
     * Returns:
     *    on success the entry that match with the id, otherwise returns zero.
     *
     */
		public function get_by_field($t, $field, $value, $type)
		{
			if ($type == 'string')
				$q = "select * from $t where $field=\"$value\"";
			else
				$q = "select * from $t where $field=$value";
				
			$r = mysql_query($q);

			for ($i = 0 ; $i < mysql_num_rows($r) ; $i++)
			{
				$rows[$i] = mysql_fetch_array($r);
			}

			return( $rows);
		}

		/**
		 * ai function		
		 *
		 * Description:      
		 *    adds a row, but without a hash generated id, it assumes the 
     *    auto_increment value on the table.
     *    
     * Params:
     *    $t - the table.
     *    $x - an array with the values to be added.
     *    
     * Returns:
     *    on success the last id generated, otherwise returns 0.
     *                                         
     */
		public function ai($t, $x)
		{
			$x = $this->order($t, $x);

			$x[0]["value"]= 0;

			$q = "insert into $t values (";

			for ($i = 0 ; $i < count($x) ; $i++)
			{
				$t = $x[$i]["type"];

				$value = $x[$i]["value"];

				$value = $this->format_value($value);

				if ($t == 'int')
					$value = (int) $value;

				if ($t == 'real' || $t == 'float')
					$value = (float) $value;

				if ($i > 0)
					$q .= ", ";

				if (strcmp($t, "string") && strcmp($t, "blob") && strcmp($t, "date") && strcmp($t, "text") && strcmp($t, "datetime"))
					$q .= " $value ";
				else
					$q .= " \"$value\" ";
			}

			$q .= ")";

			$r = mysql_query($q);

			$id = mysql_insert_id();

			return( $id);
		}

    /**
		 * add_i function		
		 *
		 * Description:      
		 *    wrapper for the ai() function, see the ai() function comment for more
		 *    details.
		 *                                      
     */
    public function add_i( $t, $x) {
      return( $this->ai( $t, $x));
    }

    /**
		 * a function		
		 *
		 * Description:      
		 *    adds a row, on a table with md5 hash generated id.
     *    
     * Params:
     *    $t - the table.
     *    $x - an array with the values to be added.
     *
     * Returns:
     *    the result          
     */
		public function a($t, $x)
		{
			$x = $this->order($t, $x);

      print_r( $x);
      
			$id = md5(uniqid(rand()));

			$x[0]["value"] = $id;

			$q = "insert into $t values (";

			for ($i = 0 ; $i < count($x) ; $i++)
			{
				$t = $x[$i]["type"];

				$value = $x[$i]["value"];

				$value = $this->format_value($value);

				if ($t == 'int')
					$value = (int) $value;
				else if ($t == 'real' || $t == 'float')
					$value = (float) $value;

				if ($i > 0)
					$q .= ", ";

				// XXX: add the other types 

				if (strcmp($t, "string") && strcmp($t, "blob") && strcmp($t, "date") && strcmp($t, "text") && strcmp($t, "datetime"))
					$q .= " $value ";
				else
					$q .= " \"$value\" ";
			}

			$q .= ")";
    
			$r = mysql_query($q);
			
      if( $r)
        return( $id);

			return( -1);
		}

    /**
		 * add function		
		 *
		 * Description:      
		 *    wrapper for the a() function, see the a() function comment for more
		 *    details.
		 *                                      
     */
    public function add( $t, $x) {
      return( $this->a( $t, $x));
    }
    
    /**
		 * a_id function		
		 *
		 * Description:      
		 *    adds a row on a table with the id specified in the $id param.
     *
     * Params:
     *    $t - the table.
     *    $x - an array with the values to be added.
     *    $id - the id of the new row.
     *                                         
     */
    public function a_id($t, $x, $id)
		{
			$x = $this->order($t, $x);

			$x[0]["value"] = $id;

			$q = "insert into $t values (";

			for ($i = 0 ; $i < count($x) ; $i++)
			{
				$t = $x[$i]["type"];

				$value = $x[$i]["value"];

				$value = $this->format_value($value);

				if ($t == 'int')
					$value = (int) $value;

				if ($i > 0)
					$q .= ", ";

				// XXX: add the other types 

				if (strcmp($t, "string") && strcmp($t, "blob") && strcmp($t, "date") && strcmp($t, "text") && strcmp($t, "datetime"))
					$q .= " $value ";
				else
					$q .= " \"$value\" ";
			}

			$q .= ")";

			$r = mysql_query( $q);

			return( $id);
		}
		
		/**
		 * add_id function		
		 *
		 * Description:      
		 *    wrapper for the a_id() function, see the a_id() function comment for more
		 *    details.
		 *
     */
		public function add_id( $t, $x, $id) {
		  return( $this->a_id( $t, $x, $id));
    }

    /**
		 * r function		
		 *
		 * Description:      
		 *    removes a row that matches with the id, from the table.
     *
     * Params:
     *    $t - the table.
     *    $id - the id of the row to be removed.
     *                                         
     */
		public function r($t, $id)
		{
			$q = "delete from $t where id=\"$id\"";
			
      mysql_query( $q);
		}

    /**
		 * remove function		
		 *
		 * Description:      
		 *    wrapper for the r() function, see the r() function comment for more
		 *    details.
		 *
     */		
		public function remove( $t, $id) {
		  $this->r( $t, $id) ;
    }

    /**
		 * m function		
		 *
		 * Description:      
		 *    modifies a row with the values provided.
     *
     * Params:
     *    $t - the table.
     *    $x - the new values for the row (an the row info).
     *
     */
		public function m($t, $x)
		{
			$bkp_values = $x;

			// fields
			$x = $this->order($t, $x);

			$q = "update $t set ";

			for ($i = 0 ; $i < count($x) ; $i++)
			{
				$t = $x[$i]["type"];

				$name = $x[$i]["name"];

				if (!isset($bkp_values[$name]))
					continue;

				$value = $x[$i]["value"];

				// replace quotes with apos.
				// with the sql format type.
				$value = $this->format_value($value);

				// removes repeat quotes
				if ($i > 0)
					$q .= ", "; 

				// XXX: add the other types 
				if (strstr($type, "int")) // integer types, XXX: add non integer types 
					$q .= " $name=$value ";
				else
					$q .= " $name=\"$value\" ";
			}

			$id = $x[0]["value"]; // always the id

			$q .= " where id=\"$id\"";

			$r = mysql_query($q);

			return( $r);
		}
    
    /**
		 * modify function		
		 *
		 * Description:      
		 *    wrapper for the m() function, see the m() function comment for more
		 *    details.
		 *                          
     */
    public function modify( $t, $x) {
      return( $this->m( $t, $x));
    }
    
    /**
		 * edit function		
		 *
		 * Description:      
		 *    wrapper for the m() function, see the m() function comment for more
		 *    details.
		 *                          
     */
    public function edit( $t, $x) {
      return( $this->m( $t, $x));
    }
	}


  /*
   * the end, where all the code becomes one
   * 	- fgabrieli
   */









// you should be working!

?>
Return current item: CMySQL