PHP function to query mySql database for a single value. Feed it the query string and ensure that the database connection has been previously established.

Syntax: string mySqlQuerySingle( string $string )

Code:

function mySqlQuerySingle($query) {
        if(!$q = mysql_query($query)) return (array());
        if(!$ret = mysql_fetch_row($q)) return "";
        return $ret[0];
}

Example (assuming the highest value in column “id” in table “table” is “47″):

$query = “SELECT MAX(id) from table”;
$ret = mySqlQuerySingle($query);
echo $ret;

Output:

47

Advertisement