Returning Database Retrieved Values
Hi,
I have a function in my database management class. What i want it to perform is that when i query the database which returns a single value, the function can do it for me.
For example:
My query is "SELECT Username FROM Customer WHERE CustomerID = 1"
This query will return a single value result.
My current function is this:
public function executeScalar($aQuery) {
$this->dbResult = mysql_query($aQuery,$this->getDBConnection()) ;
$this->dbDataRow = mysql_fetch_row($this->dbResult) ;
$this->retrievedValue = $this->dbDataRow[0] ;
return $this->retrievedValue ;
}
I have other functions such as:
- connect() : This function connect to the database with the login information
- close() : This function closes the connection to the database
- getDBConnection() : This function returns the current database connection
- isConnected() : This function returns boolean variable whether there is connectivity to the database.
I hope someone can assist me with this. Besides this, I also need a function to perform commands such as UPDATE, DELETE and INSERT.
The function would actually returns the number of rows affected in the query. Could someone please assist me in that function? This is because I've tried many ways and I could not derive at the solution.
The function header is:
public function executeNonQuery($aQuery) {
//Returns an integer indicating the number of rows affected in this query
}
Thank you.
Re: Returning Database Retrieved Values
Quote:
int mysql_affected_rows([resource link_identifier])
mysql_affected_rows() returns the number of rows affected by the last INSERT, UPDATE or DELETE query associated with link_identifier. If the link identifier isn't specified, the last link opened by mysql_connect() is assumed.
So...
PHP Code:
public function executeNonQuery($aQuery) {
$this->dbResult = mysql_query($aQuery,$this->getDBConnection());
// either store value or return:
return mysql_affected_rows($this->getDBConnection());
}
Re: Returning Database Retrieved Values
Are you asking for help on all of the functions? Or just the one that shows the affected rows? Simply, for affected rows, you can use mysql_affected_rows()
Good luck;