Jump to content


Photo

please help


  • Please log in to reply
1 reply to this topic

#1 dizyn

dizyn
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 30 September 2006 - 08:49 AM

Please see this code and help me how
$this->sql = $q;
$this->log();
$r = mysql_query($q);
if (!($r)) {
$this->error_log();
}
$num=mysql_num_rows($r);
$i=1;
while ($row=mysql_fetch_object($r)) {
$cont[$i] = $row;
$i++;
}

will work. I am new to OOP it would be good if someone explain this bunch of code to me.
thanks much.
<?php 
class Dbase
{

	function Dbase(){
		include ("connection.php");
		$this->dbLink=$dbLink;
	} 

	function select($retField, $table, $where="", $groupby="", $orderby="", $limit="") {
		$fields = implode(",", $retField);
		if ($where!="") {
			$q = "select $fields from $table WHERE $where";
		} else {
			$q = "select $fields from $table";
		}
		if ($groupby!="") {
			$q .= " GROUP BY $groupby";
		}
		if ($orderby!="") {
			$q .= " ORDER BY $orderby";
		}
		if ($limit!="") {
			$q .= " LIMIT $limit";
		}

		//return $q;
		//if($orderby == 'ts_datAdded DESC'){
		//	echo $q."<br><br>";exit;}
		$this->sql = $q;
		$this->log();
		$r = mysql_query($q);
		if (!($r)) {
			$this->error_log();
		}
		$num=mysql_num_rows($r);
		$i=1;
		while ($row=mysql_fetch_object($r)) {
			$cont[$i] = $row;
			$i++;
		}
		if (mysql_num_rows($r)>0) {
			
		//	echo print_r($cont);
		//	exit;
			
			return $cont;
		}
	}
}
?>

mod edit : Please use code tags

#2 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,430 posts
  • LocationBelgium

Posted 30 September 2006 - 12:10 PM

<?php
// Declare the class 'Dbase'
class Dbase
{

	function Dbase(){ // Class constructor
		include ("connection.php"); // strange place to include a file
		$this->dbLink=$dbLink; // this adds the $dbLink to the class property dbLink
	} 

	function select($retField, $table, $where="", $groupby="", $orderby="", $limit="") {
		$fields = implode(",", $retField);
		if ($where!="") {
			$q = sprintf("select %s from %s WHERE %s", $fields, $table, $where); // makes it a bit more secure
		} else {
			$q = sprintf("select %s from %s", $fields, $table);
		}
		if ($groupby!="") {
			$q .= sprintf(" GROUP BY %s", $groupby); // adds a group by to the current query (put duplicates together)
		}
		if ($orderby!="") {
			$q .= sprintf(" ORDER BY %s", $orderby); // adds a order by to the current query (orders the result by $orderby)
		}
		if ($limit!="") {
			$q .= sprintf(" LIMIT %s", $limit); // limits the result to a certain number
		}

		//return $q;
		//if($orderby == 'ts_datAdded DESC'){
		//	echo $q."<br><br>";exit;}
		$this->sql = $q; // adds the query to the class property sql
		$this->log(); // executes class method log(); , however this method is not in the current class
		$r = mysql_query($q); // sends the current sql syntax to the database for execution , however why do you use $q while you assigned it to $this->sql ??
		if (!($r)) { // checks if mysql_query(); was executed succesfully , if (!($r = mysql_query($q)) , however i also would assign $r to a class property
			$this->error_log(); // executes the class method error_log();
		}
		$num=mysql_num_rows($r); // counts the total rows returned
		$i=1;
		while ($row=mysql_fetch_object($r)) { // why do you fetch it as an object when you assign it to an array ?
			$cont[$i] = $row;
			$i++;
		}
		if (mysql_num_rows($r)>0) { // double work , use your declared/initialised $num instead , if ($num > 0) ...
			
		//	echo print_r($cont);
		//	exit;
			
			return $cont;
		}
	}
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users