help me write php functions to retrieve data from database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
satyagode
Forum Newbie
Posts: 1
Joined: Mon Sep 06, 2004 5:23 am

help me write php functions to retrieve data from database

Post by satyagode »

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hii friends,

Happy to join this group which is very active.

I would like to request, if any one could help me write php functions that take mySQL querries....and return results. I already has two php classes that initiate the connection to the database.........

All i need is write a [b]single php class[/b] that can have all the querries required in it, the two classes that i have are [i]connect_db.php[/i] and [i]query_db.php

Please tell me if these classes would be helpfull for me to add the finctions i need in the query.php given below.

[/i]

Code: Select all

/************************************************************
CLASS FOR ACCESS MYSQL DATABASES
************************************************************/
<?
	class clsDB {

		var $host;
		var $port;
		var $user;
		var $pass;
		var $database;

		var $connection;
		var $retVal;
		
		var $debugMode;

		var $timer_start;
		var $timer_end;
		var $elapsed_time;

		var $total_queries;

		

		/*	CONSTRUCTOR  */
		function clsDB(){
			$this->connection = 0;
			$this->retVal     = 0;
			$this->port       = 3306;
			$this->debugMode  = false;
		}


		/* properties */

		function setUsername($sUsername){
			$this->user = $sUsername;
		}

		function setPassword($sPassword){
			$this->pass = $sPassword;
		}

		function setHost($sHost, $iPort = 3306){
			$this->host = $sHost;
			$this->port = $iPort;
		}

		function setDatabase($sDatabase){
			$this->database = $sDatabase;
		}

		function setDebugMode($bDebugmode){
			$this->debugMode = $bDebugmode;
		}

		function getTotalQueries(){
			return $this->total_queries;
		}
		
		/* properties */


		/* methods */

		function openDB(){
			$this->connection = mysql_connect($this->host . ":" . 
$this->port,$this->user,$this->pass) or die("erro ao conectar-se no servidor...");

			mysql_select_db($this->database, $this->connection);

				return $this->connection;
		}

		function closeDB(){
			mysql_close($this->connection) or die("erro ao fechar a 
coneccao...");
		}
		
		function closeRS(){
			if ( $this->retVal != 0 && $this->retVal != 1 ) {
				mysql_free_result($this->retVal) or die("erro ao fechar o 
resultset");
			}
		}
		
		function ExecuteSQL($strSQL){
			
			$this->closeRS();
			
			$this->startWatch();
			
			// excuta a strSQL
			$this->retVal = mysql_query($strSQL, $this->connection) or die 
(mysql_error()."<br><b>".$strSQL."</b>");
			// executa a strSQL

			$this->stopWatch();

			if ( $this->debugMode )
				echo $this->elapsed_time . " - <b>" . $strSQL . "</b><br>\n";

			$this->total_queries++;

				return $this->retVal;
		
		}
		
		function isRsEmpty(){
				return (!mysql_num_rows($this->retVal)) ? 1:0 ;
		}

		function getRows(){
			$num_fields = mysql_num_fields($this->retVal); 
			$x=0;
			while($row=mysql_fetch_array($this->retVal)){
				for($j=0;$j<$num_fields;$j++){
					$name = mysql_field_name($this->retVal, $j);
					$objectї$x]ї$name]=$rowї$name];
				}
				$x++;
			}
			if ( $x > 0 ) {
				return $object;
			}
		}

		/* methods */
		/* funcoes internas */
		function startWatch(){
			list($foo,$bar)=explode(' ',microtime());
			$this->timer_start=$foo+$bar;
			unset($foo);
			unset($bar);
		}
		
		function stopWatch(){
			list($foo,$bar)=explode(' ',microtime());
			$this->timer_end=$foo+$bar;
			unset($foo);
			unset($bar);
				$this->elapsed_time = round($this->timer_end-$this->timer_start,5);
		}
		/* funcoes internas */
	} /* class */
	function openDefaultDB(){
		
		$db = new clsDB;

		$db->setUsername("root");
		$db->setPassword("");
		$db->setHost("localhost");
		$db->setDatabase("DATABASENAME");
		$db->setDebugmode(true);
		

		$db->openDB();
		return $db;
	}
?>

Code: Select all

*************************************************
Class that holds the database information and the sql querries
*************************************************

<?

	require "cDB.php";

		$host = "HOST";
		$user = "USERNAME";
		$pass = "PASSWORD";

	$db = new clsDB();
		$db->setHost($host);
		$db->setUsername($user);
		$db->setPassword($pass);
		$db->setDatabase("DBNAME");
		
		$db->setDebugmode(true);

		$db->openDB();

                           /*sql querries to access the data from the database  come here*/
		$strSQL = "SELECT * FROM TABLE_NAME WHERE ID = 1;";
		$query = $db->ExecuteSQL($strSQL);
		$row = mysql_fetch_array($query);
		$theValue = $rowї'rowNAME'];

?>

Thanq very much,
Sathya.


feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Post Reply