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;
}
}
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.
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.