Jump to content

Recommended Posts

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.
[code]
<?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;
}
}
}
?>
[/code]

mod edit : Please use code tags
Link to comment
https://forums.phpfreaks.com/topic/22577-please-help/
Share on other sites

[code]
<?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;
}
}
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/22577-please-help/#findComment-101339
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.