1 Replies - 686 Views - Last Post: 07 April 2010 - 02:35 PM Rate Topic: -----

#1 rmccarter721   User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 139
  • Joined: 10-September 07

Optimize Class

Posted 07 April 2010 - 08:18 AM

Hey Guys,

I am developing my own database class, everything works, though can anyone suggest any ways to optimize it or any ways I can improve this script?

Cheers in advance

<?php

class DB{
	var $RQuery,
	$NumRows;
	Function DBConnect(){
		GLOBAL $DBLogin;
		$login = explode ('|', $DBLogin);
		mysql_connect($login[0], $login[1], $login[2]);
		mysql_select_db($login[3]);
	}
	Function DBClose(){
		mysql_close();
	}
	Function SelectData($fields, $table, $criteria='', $order=''){
		#
		#$DataBaseConnection = new DB;
		#$DataBaseConnection->SelectData('FIELDS', 'TABLE', 'CRITERIA', 'ORDER');
		#
		#FIELDS, TABLE, CRITERIA, ORDER
		#
		$this->DBConnect();
		if($fields == NULL || $table == NULL){
			echo 'Invalid input to SelectData';
			die;
		}
		if ($criteria != ''){
			$criteria = ' WHERE '.$criteria;
		}
		if ($order != ''){
			$order = ' ORDER BY '.$order;
		}

		$result = mysql_query("SELECT $fields FROM $table $criteria $order")
			or die("Error in SelectData: " . mysql_error());
		$this->NumRows = mysql_num_rows($result);
		if($this->NumRows > 1){
			$i=0;
			while ($midput = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$this->RQuery[$i]= $midput;
				$i++;
			}
		}
		elseif($this->NumRows == 1){
			$this->RQuery = mysql_fetch_array($result);
		}
		else{
			return 'No Results Returned in SelectData';
			die;
		}
		$this->DBClose();
	}
	Function InsertData($table, $columns, $values){
		$this->DBConnect();
		if($table == NULL || $columns == NULL || $values == NULL){
			echo 'Invalid input to InsertData';
			die;
		}
		mysql_query("INSERT INTO $table ($columns) VALUES ($values)")
			or die("Error in InsertData: " . mysql_error());
		$this->DBClose();
	}
	Function UpdateData($table, $columns, $values, $criteria){
		#
		#$columns and $values are linked arrays
		#$columns[0] corresponds to $values[0]
		#$columns[1] corresponds to $values[1] etc.		#
		#
		$this->DBConnect();
		if($table == NULL || $columns == NULL || $values == NULL || $criteria == NULL){
			echo 'Invalid input to UpdateData';
			die;
		}
		if(count($columns) != count($values)){
			echo 'UpdateData Error: Unequal Number of Columns and Values entered';
			die;
		}
		else{
			$NumEntries = count($columns);
		}
		$a=0; $b=1; $set ='';
		while ($b <= ($NumEntries-1)){
			$set .= '`'.$columns[$a].'` = "'.$values[$a].'", ';
			$a++;$b++;
		}
		$set .= '`'.$columns[$a].'` = "'.$values[$a].'"';
		mysql_query("UPDATE $table SET $set WHERE $criteria")
			or die("Error in UpdateData: " . mysql_error());
		$this->DBClose();
	}
	Function DeleteData($table, $criteria){
		$this->DBConnect();
		if($table == NULL || $criteria == NULL){
			echo 'Invalid input to DeleteData';
			die;
		}
		mysql_query("DELETE FROM $table WHERE $criteria");
		$this->DBClose();
	}
	Function CreateTable($table, $fields){
		$this->DBConnect();
		if($table == NULL || $fields == NULL){
			echo 'Invalid input to CreateTable';
			die;
		}
		mysql_query("CREATE TABLE $table ($fields)");
		$this->DBClose();
	}
}

?>


Is This A Good Question/Topic? 0
  • +

Replies To: Optimize Class

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3839
  • View blog
  • Posts: 14,005
  • Joined: 08-August 08

Re: Optimize Class

Posted 07 April 2010 - 02:35 PM

Here's one thing. This:
                $this->NumRows = mysql_num_rows($result);
                if($this->NumRows > 1){
                        $i=0;
                        while ($midput = mysql_fetch_array($result, MYSQL_ASSOC)) {
                                $this->RQuery[$i]= $midput;
                                $i++;
                        }
                }
                elseif($this->NumRows == 1){
                        $this->RQuery = mysql_fetch_array($result);
                }


could shorten to this:
unset($this->RQuery);
while ($midput = mysql_fetch_array($result, MYSQL_ASSOC)) {
	$this->RQuery[]= $midput;
}
$i = count($this->RQuery);


Not sure why you want $i in there, so you may be able to remove the last line.

This post has been edited by CTphpnwb: 07 April 2010 - 02:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1