/    Sign up×
Community /Pin to ProfileBookmark

PHP Classes and MySQL – Increasing Efficiency

I’ve started implementing classes to help reduce code but I’m wondering if there is an efficient way to retrieve data from multiple columns for one row

For example:

I have a table (emp_table) for employees and in the table is thus:

| id |name | age | sex |
|- – – – – – – – – – – – – – – |
| 1001 | John | 26 | m |
| 1002 | Bob | 30 | m |

If I have an Employee class:

[code=php]
class Employee {
var $DB_EMP = 0;

// Set the Employee ID.
function set($id){
$this->DB_EMP = $id;
}

// Main employee query.
function emp(){
$query = “SELECT * FROM emp_table WHERE id=’$this->DB_EMP’ “;
$result = mssql_query($query) or die(mysql_error());
return $result;
}

// Get name.
function name(){
$result = $this->property();
if(!mssql_num_rows($result)){
$output = ‘Error’;
}else{
$output = mssql_result($result, $i, ‘name’);
}
return $output;
}

// Get age.
function age(){
$result = $this->property();
if(!mssql_num_rows($result)){
$output = ‘Error’;
}else{
$output = mssql_result($result, $i, ‘age’);
}
return $output;
}

// Get sex.
function sex(){
$result = $this->property();
if(!mssql_num_rows($result)){
$output = ‘Error’;
}else{
$output = mssql_result($result, $i, ‘sex’);
}
return $output;
}
}
[/code]

Impented like this:

[code=php]

$emp = new Employee;

$emp->set(1001); // User ID for John
echo $emp->age(); // print Johns age.
echo $emp->name(); // print Johns name.
echo $emp->sex(); // print Johns sex.

[/code]

The above is a rough extract of my class but as you see above, each request for either name, age or sex will re-run the query in the emp() function.

My real class has about 40 functions to retireve specific information from about 40 columns for a specific record. So the example is just a simple illustration of my problem.

Is there a more efficient way to do what I’m trying to do?

Feel free to ask for more info if I’m not clear enough.

Thanks,

P.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiJul 24.2009 — How about something like this:

[code=php]
class Employee {

private $id;
private $data = array();

public function __construct($id = null) {
if ($id) {
$this->id = $id;
$this->_loadData();
}
}

public function set($id) {
$this->id = $id;
$this->_loadData();
}

public function __get($name) {
return (isset($this->data[$name])) ? $this->data[$name] : null;
}

private function _loadData() {
try {
$this->data = $this->_query($this->id);
} catch(Exception $e) {
trigger_error($e->getMessage(), E_USER_ERROR);
}
}

private function _query($id) {
$query = "SELECT * FROM emp_table WHERE id = {$id} LIMIT 1";
if (!$result = mssql_query($query)) {
throw new Exception('Could not retrieve employee information');
} else {
return mssql_fetch_assoc($result);
}
}

}

$emp = new Employee(1001);
echo $emp->name; // Output: John
[/code]


I'd also recommend using a database abstraction layer rather than calling DBMS-specific functions from inside your methods.
×

Success!

Help @paperclip spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...