1 record to return from SQL query
Posted: Tue Dec 05, 2006 4:33 pm
I always end up having a lot of methods in my classes where I'm returning 1 record.
For instance (I'll assume you can anticipate what the methods would actually look like)
So, now I have 2 methods. Seems kind of bloated to me.
As a way to save space, I'll sometimes do if statements and add an argument:
So, in the second example, I have 1 method, but the code is still kind of verbose.
I'm kind of thinking I want to start doing something like this on methods that return 1 value:
Then in the actual method, I could make it look like this:
Is this a bad idea, or is it something that's kind of standard?
For instance (I'll assume you can anticipate what the methods would actually look like)
Code: Select all
$firstname = $myClass->getFirstName($id);
$lastname = $myClass->getLastName($id);As a way to save space, I'll sometimes do if statements and add an argument:
Code: Select all
$firstname = $myClass->getName($id, FIRST);
$lastname = $myClass->getName($id, LAST);I'm kind of thinking I want to start doing something like this on methods that return 1 value:
Code: Select all
$firstname = $myClass->singleValueQuery($tablename, firstname, $id);
$lastname = $myClass->singleValueQuery($tablename, lastname, $id);Code: Select all
function singleValueQuery($tbl, $field) {
$this->query = "SELECT " . $field . " FROM " . $tbl . " WHERE id=" . $id;
$this->SQL = mysql_query($this->query, $this->link);
while($this->row = mysql_fetch_array($this->SQL))
return $this->row['".$field."'];
}