Voting

Please answer this simple SPAM challenge: one minus one?
(Example: nine)

The Note You're Voting On

scott_carney at hotmail dot com
6 years ago
Just thought I'd share these helper functions that I use to simplify processing of query results a bit:
<?php
// For a simple query that should return a single value, this returns just that value (or FALSE) so you can process it immediately
function db_result_single($result) {
    return (
$row = mysql_fetch_row($result)) && isset($row[0]) ? $row[0] : false;
}

// Returns the rows as an array of rows
// Providing a key_column gives you access to specific rows (e.g. if (isset($result_array[$user_id])) ...)
function db_result_array($result, $key_column = null) {
    for (
$array = array(); $row = mysql_fetch_assoc($result); isset($row[$key_column]) ? $array[$row[$key_column]] = $row : $array[] = $row);
    return
$array;
}

// Returns an array of a single column of data that can optionally be keyed from second column (e.g. an array of user names keyed by user id)
function db_result_array_values($result) {
    for (
$array = array(); $row = mysql_fetch_row($result); isset($row[1]) ? $array[$row[1]] = $row[0] : $array[] = $row[0]);
    return
$array;
}
?>

Naturally, comments [to my email, not here] are welcome.

<< Back to user notes page

To Top