sometimes a cigar is just a cigar
Header image

Php database class

Posted by ALonon in Php

I’m using this class to my little framework.

  • You can insert a data with sending column name and a array $data['mysql_field'] = ‘insert_value’,
  • Update data with sending column,array and where option
  • Delete data sending cloumn name and where option
  • @q_fetch_all : Return all data with information in column, just take sql.
  • @q_result : Taking: sql, order and field.
<?php
class main {
	static $query_array = array ();
	protected function q_query($sql) {
			$query = mysql_query ( $sql ) or die(mysql_error());
			if (! $query) {
				$message = 'Invalid query: ' . mysql_error () . "\n";
				$message .= 'Whole query: ' . $query;
				die ( $message );
			}

			return $query;

	}

	static function q_update($column, $data, $where) {
		foreach ($data as $form_name => $value) {
			$data_set .= $form_name."='".mysql_real_escape_string(htmlspecialchars(stripslashes($value)))."',";
		}
		$data_set = substr($data_set,0,-1);
		return self::q_query ( "update $column set $data_set $where" );
	}

	protected function q_delete($column, $where) {
		return  self::q_query ( "delete from $column $where" );
	}

	protected function q_insert($column, $data) {
		foreach ( $data as $form_name => $value ) {
			$values [] =  mysql_real_escape_string(htmlspecialchars(stripslashes($value)));
			$column_name [] = $form_name;
		}
		$postdata = "'" . implode ( "','", $values ) . "'";
		$columnname = implode ( ', ', $column_name );
		return  self::q_query ("INSERT INTO $column($columnname) VALUES ($postdata)");
	}

	public static function q_num_row($sql) {
		$query = self::q_query ( $sql );
		return @mysql_num_rows ( $query );
	}

	public static function q_fetch_all($sql) {
		$query = self::q_query ( $sql );
		while ( $result [] = mysql_fetch_assoc ( $query ) ) {
		}
		array_pop ( $result );
		return $result;
	}

	public static function q_fetch_array($sql) {
		$query = self::q_query ( $sql );
		return @mysql_fetch_array ( $query );
	}

	public static function q_fetch_assoc($sql) {
		$query = self::q_query ( $sql );
		return mysql_fetch_assoc ( $query );
	}

	public static function q_result($sql, $order = '0', $field) {
		$query = self::q_query ( $sql );
		return @mysql_result ($query, $order, $field );
	}
}
?>

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>