Jump to content

Recommended Posts

I wrote this class to handle my querys, I am new to OO and was just wondering if the numbering to identify the querys would slow it down? I think I will need to have multiple querys active at any given time.

 

<?php
class db
{
function db($host = 'localhost', $user = '', $pass = '', $DB = '')
{
  $conn = mysql_connect($host, $user, $pass);
  $db   = mysql_select_db($DB, $conn);
}
function query($str, $num = 1)
{
  $this->query{$num} = mysql_query($str)or die('Query failed on line ' . __LINE__);
}
function num($num = 1)
{
  return mysql_num_rows($this->query{$num});
}
function obj($num = 1)
{
  return mysql_fetch_object($this->query{$num});
}
function assoc($num = 1)
{
  return mysql_fetch_assoc($this->query{$num});
}
function row($num = 1)
{
  return mysql_fetch_row($this->query{$num});
}
function arr($num = 1)
{
  return mysql_fetch_array($this->query{$num});
}

}
?>

Link to comment
https://forums.phpfreaks.com/topic/143696-solved-database-class/
Share on other sites

I rewrote your class a little bit just because I felt like it :P

 

<?php

class DB {

private $conn;
private $result;

private $user;
private $host;
private $pass;
private $db;

public $queries;

public function __construct($host = 'localhost', $user = '', $pass = '', $db = '') 
{
	$this->user = $user;
	$this->pass = $pass;
	$this->host = $host;
	$this->db   = $db;

	$this->conn = mysql_connect($this->host, $this->user, $this->pass);
	mysql_select_db($this->db, $this->conn);
}

function query($query = '')
{
	$this->queries[] = $query;
	$this->result = mysql_query($str)or die('Query failed on line ' . __LINE__);
}

function num()
{
	return mysql_num_rows($this->result);
}

function obj()
{
	return mysql_fetch_object($this->result);
}

function assoc()
{
	return mysql_fetch_assoc($this->result);
}

function row($num = 1)
{
	return mysql_fetch_row($this->result);
}

function arr($num = 1)
{
	return mysql_fetch_array($this->result);
}

}
?>

 

But to answer your question, no it wouldn't slow it down, but I challenge you to think of a situation in which you need to do what you are suggesting.

TBH, I just couldn't be bothered to think of loads of variable names and end up writing another script with irrelevant vars, i.e. $retard = $row[4];

 

lol

 

So I wrote it like that so I could save my queries via a variable id inside the class.

 

I suppose you make a good point tho lol

 

also in your class rewrite you forgot to replace $str =P

 

And the db user, pass, host, db vars dont need to be accessed anywhere apart from where they are defined so I dont think $this->user etc... Is irrelevant?

 

Please correct me if I am wrong :S

And the db user, pass, host, db vars dont need to be accessed anywhere apart from where they are defined so I dont think $this->user etc... Is irrelevant?

 

You're right, but if I was writing this class, I would be interested in adding error handling to more than just the constructor, in which case you want know more information about the current state of things.  I would also be interested in writing some other methods, like, connect(), lastQuery() etc.  Also, for documentation purposes, it is super helpful to explicitly declare your member variables so that someone else looking at the class can quickly see what they're working with.

Does your class even work Andy-H? This method....

 

function query($str, $num = 1)
{
  $this->query{$num} = mysql_query($str)or die('Query failed on line ' . __LINE__);
}

 

Your overiding the method itself with a property which I assume is meant to be an array. Arrays use [] to hold there elements. Ive not tested you code but that little snippet there looks like it should generate an error. At very least it should bring about some very unexpected results.

 

And the db user, pass, host, db vars dont need to be accessed anywhere apart from where they are defined so I dont think $this->user etc... Is relevant?

 

Its relevent because non of that stuff should actually be defined within your class. if that was the case you would need to edit the class itself every time you wanted to use it iin a different project. This goes against OOP principles.

 

I have an entire framework that can run mutliple sites from the same code base. If I had to edit one of the classes itself to add my username / password and server details I would be screwed.

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.