PDA

View Full Version : Common SQL functions


Mathias
August 18th, 2007, 08:21 PM
Note, all of my functions belong to classes, but I am posting only snippets of my functions to avoid boredom and giving away all my secrets. Please post corrections, suggestions, and your own related functions.

Mathias
August 18th, 2007, 08:39 PM
//-------------------------------------------------------------------------
// Function: mysqlSelect
// Author: Andrew Hinkle
// Modified: 06/20/2007
// Description: Return the select query results in an array.
//-------------------------------------------------------------------------
public function mysqlSelect($sSQL)
{
// Was the database connected?
if (empty($this->_pConnect)) return 0;

// Run the query and capture the results.
$mResult = mysql_query($sSQL);

// Return an empty array if no results were returned.
if (empty($mResult)) return array();

// Convert the mysql result to a standard array.
$aResult = array();
while ($aFetched = @mysql_fetch_array($mResult, MYSQL_ASSOC))
{
// Append the fetched array to the result array.
$aResult[] = $aFetched;
}

// mysql memory clean up.
@mysql_free_result($mResult);

return $aResult;
}

Mathias
August 18th, 2007, 08:40 PM
//-------------------------------------------------------------------------
// Function: mysqlQuery
// Author: Andrew Hinkle
// Modified: 06/20/2007
// Description: Run the query on the table, but do not return any results.
//-------------------------------------------------------------------------
public function mysqlQuery($sSQL)
{
// Was the database connected?
if (empty($this->_pConnect)) return (0);

// Run the query and capture the results.
$mResult = mysql_query($sSQL);

// Results were not returned.
if (empty($mResult)) return 0;

// mysql memory clean up.
@mysql_free_result($mResult);

// Results were returned and freed.
return 1;
}