Exemplo n.º 1
0
function db_handler($query, $output = "result", $debug_title = "query")
{
    # Remove beginning spaces
    $query = trim($query);
    if (DB_NO_WRITES == 1 and !preg_match("/^SELECT/i", $query)) {
        message('INFO', "DB_NO_WRITES activated, no deletions or modifications will be performed");
    } else {
        $result = mysql_query($query);
        // new DEBUG output
        $debug_query = NConf_HTML::text_converter("sql_uppercase", $query);
        $debug_query_output = NConf_HTML::swap_content($debug_query, 'Query', FALSE, FALSE);
        $debug_data_result = '<br>(Result output not yet defined)';
        if ($result) {
            # Output related stuff
            # not already implemented, or replaced functions:
            //if ($output == "getOne") $output = "1st_field_data";
            switch ($output) {
                case "affected":
                case "insert":
                case "update":
                case "delete":
                    $affected = mysql_affected_rows();
                    if ($affected > 0) {
                        //message('DEBUG', "# affected rows: $affected", "ok");
                        $return = $affected;
                    } else {
                        // needed for inserts ??:
                        //message('DEBUG', "# affected rows: $affected", "nomatch");
                        $return = "yes";
                    }
                    # DEBUG output with new API module:
                    $debug_data_result = NConf_HTML::swap_content($return, 'Result: affected rows: ' . $affected . ')', FALSE, TRUE);
                    break;
                case "getOne":
                    $first_row = mysql_fetch_row($result);
                    $return = $first_row[0];
                    # DEBUG output with new API module:
                    $debug_data_result = NConf_HTML::text('<b>Result: getOne:</b>' . $return);
                    break;
                case "result":
                    $return = $result;
                    # DEBUG output with new API module:
                    $debug_data_result = NConf_HTML::text('<b>Result:</b>' . $return);
                    break;
                case "query":
                    $return = $query;
                    # DEBUG output with new API module:
                    $debug_data_result = NConf_HTML::text('<b>Result: see query above</b>');
                    break;
                case "insert_id":
                    $new_id = mysql_insert_id();
                    $return = $new_id;
                    # DEBUG output with new API module:
                    $debug_data_result = NConf_HTML::text('<b>Result: last query generated ID:</b>' . $return);
                    break;
                case "num_rows":
                    $result = mysql_num_rows($result);
                    $return = $result;
                    # DEBUG output with new API module:
                    $debug_data_result = NConf_HTML::text('<b>Result: number of rows:</b>' . $return);
                    break;
                case "assoc":
                    $result = mysql_fetch_assoc($result);
                    $return = $result;
                    # DEBUG output with new API module:
                    $debug_data_result = NConf_HTML::swap_content($return, 'Result: assoc array:', FALSE, TRUE);
                    break;
                case "array":
                    $i = 0;
                    $rows = array();
                    while ($row = mysql_fetch_assoc($result)) {
                        $rows[$i] = $row;
                        $i++;
                    }
                    $count = count($rows);
                    $return = $rows;
                    # DEBUG output with new API module:
                    $debug_data_result = NConf_HTML::swap_content($return, 'Result array (rows: ' . $count . ')', FALSE, TRUE);
                    break;
                case "array_direct":
                    $i = 0;
                    $rows = array();
                    while ($row = mysql_fetch_row($result)) {
                        $rows[$i] = $row[0];
                        $i++;
                    }
                    $count = count($rows);
                    $return = $rows;
                    # DEBUG output with new API module:
                    $debug_data_result = NConf_HTML::swap_content($return, 'Result array_direct (rows: ' . $count . ')', FALSE, TRUE);
                    break;
                case "array_2fieldsTOassoc":
                    $rows = array();
                    while ($row = mysql_fetch_row($result)) {
                        $rows[$row[0]] = $row[1];
                    }
                    $count = count($rows);
                    $return = $rows;
                    # DEBUG output with new API module:
                    $debug_data_result = NConf_HTML::swap_content($return, 'Result array_2fieldsTOassoc (rows: ' . $count . ')', FALSE, TRUE);
                    break;
                    # Failed on output case
                # Failed on output case
                default:
                    message('ERROR', "db_handler failed on output case");
                    return FALSE;
            }
            // Debug and result return
            $debug_entry = NConf_HTML::swap_content($debug_query_output . $debug_data_result, "<b>SQL</b> " . $debug_title, FALSE, FALSE, 'debbug_query');
            message('DEBUG', $debug_entry);
            return $return;
        } else {
            // makes an open debug entry with mysql_error info
            $debug_entry = NConf_HTML::swap_content($debug_query_output . '<br><b>mysql error:</b>' . mysql_error(), '<b class="attention" >SQL</b> ' . $debug_title, TRUE, FALSE, 'debbug_query color_warning');
            NConf_DEBUG::set($debug_entry, 'DEBUG');
        }
    }
}