Ejemplo n.º 1
0
/**
 * Perform a DB Layer SQL query.
 *
 * This function returns values dependin on the input parameters and the result of the query.
 * It can return:
 *   false or a string if there was an error (depends on $expectError),
 *   true if the query succeeded but did not generate any rows
 *   array of field values if it returned a single row and $single is true
 *   array of array of field values if it returned row(s) [stacked array]
 *
 * @access public
 * @param   string      SQL query to execute
 * @param   boolean     Toggle whether the expected result is a single row (TRUE) or multiple rows (FALSE). This affects whether the returned array is 1 or 2 dimensional!
 * @param   string      Result type of the array indexing. Can be one of "assoc" (associative), "num" (numerical), "both" (numerical and associative, default)
 * @param   boolean     If true, errors will be reported. If false, errors will be ignored.
 * @param   string      A possible array key name, so that you can control the multi-dimensional mapping of an array by the key column
 * @param   string      A possible array field name, so that you can control the multi-dimensional mapping of an array by the key column and the field value.
 * @param   boolean     If true, the executed SQL error is known to fail, and should be disregarded (errors can be ignroed on DUPLICATE INDEX queries and the likes)
 * @return  mixed       Returns the result of the SQL query, depending on the input parameters
 */
function &serendipity_db_query($sql, $single = false, $result_type = "both", $reportErr = false, $assocKey = false, $assocVal = false, $expectError = false)
{
    global $serendipity;
    $type_map = array('assoc' => PDO::FETCH_ASSOC, 'num' => PDO::FETCH_NUM, 'both' => PDO::FETCH_BOTH, 'true' => true, 'false' => false);
    //serendipity_db_logmsg('SQLQUERY: ' . $sql);
    if (!$expectError && ($reportErr || !$serendipity['production'])) {
        $serendipity['dbSth'] = $serendipity['dbConn']->prepare($sql);
    } else {
        $serendipity['dbSth'] = $serendipity['dbConn']->prepare($sql);
    }
    if (!$serendipity['dbSth']) {
        if (!$expectError && !$serendipity['production']) {
            print "<span class='msg_error'>Error in {$sql}</span>";
            print $serendipity['dbConn']->errorInfo() . "<BR/>\n";
            if (function_exists('debug_backtrace')) {
                highlight_string(var_export(debug_backtrace(), 1));
            }
            print "<pre>{$sql}</pre>";
        }
        return $type_map['false'];
    }
    $serendipity['dbSth']->execute();
    if ($serendipity['dbSth'] === true) {
        return $type_map['true'];
    }
    $result_type = $type_map[$result_type];
    $rows = array();
    foreach ($serendipity['dbSth']->fetchAll($result_type) as $row) {
        $row = serendipity_db_sqlite_fetch_array($row, $result_type);
        if (!empty($assocKey)) {
            // You can fetch a key-associated array via the two function parameters assocKey and assocVal
            if (empty($assocVal)) {
                $rows[$row[$assocKey]] = $row;
            } else {
                $rows[$row[$assocKey]] = $row[$assocVal];
            }
        } else {
            $rows[] = $row;
        }
    }
    //serendipity_db_logmsg('SQLRESULT: ' . print_r($rows,true));
    if (count($rows) == 0) {
        if ($single) {
            return $type_map['false'];
        }
        return $type_map['true'];
    }
    if (count($rows) == 1 && $single) {
        return $rows[0];
    }
    return $rows;
}
Ejemplo n.º 2
0
/**
 * Perform a DB Layer SQL query.
 *
 * This function returns values dependin on the input parameters and the result of the query.
 * It can return:
 *   false or a string if there was an error (depends on $expectError),
 *   true if the query succeeded but did not generate any rows
 *   array of field values if it returned a single row and $single is true
 *   array of array of field values if it returned row(s) [stacked array]
 *
 * @access public
 * @param   string      SQL query to execute
 * @param   boolean     Toggle whether the expected result is a single row (TRUE) or multiple rows (FALSE). This affects whether the returned array is 1 or 2 dimensional!
 * @param   string      Result type of the array indexing. Can be one of "assoc" (associative), "num" (numerical), "both" (numerical and associative, default)
 * @param   boolean     If true, errors will be reported. If false, errors will be ignored.
 * @param   string      A possible array key name, so that you can control the multi-dimensional mapping of an array by the key column
 * @param   string      A possible array field name, so that you can control the multi-dimensional mapping of an array by the key column and the field value.
 * @param   boolean     If true, the executed SQL error is known to fail, and should be disregarded (errors can be ignored on DUPLICATE INDEX queries and the likes)
 * @return  mixed       Returns the result of the SQL query, depending on the input parameters
 */
function &serendipity_db_query($sql, $single = false, $result_type = "both", $reportErr = true, $assocKey = false, $assocVal = false, $expectError = false)
{
    global $serendipity;
    $type_map = array('assoc' => SQLITE_ASSOC, 'num' => SQLITE_NUM, 'both' => SQLITE_BOTH, 'true' => true, 'false' => false);
    static $debug = false;
    if ($debug) {
        // Open file and write directly. In case of crashes, the pointer needs to be killed.
        $fp = @fopen('sqlite.log', 'a');
        fwrite($fp, '[' . date('d.m.Y H:i') . '] SQLITE QUERY: ' . $sql . "\n\n");
        fclose($fp);
    }
    if ($reportErr && !$expectError) {
        $res = sqlite_query($sql, $serendipity['dbConn']);
    } else {
        $res = @sqlite_query($sql, $serendipity['dbConn']);
    }
    if (!$res) {
        if (!$expectError && !$serendipity['production']) {
            var_dump($res);
            var_dump($sql);
            $msg = "problem with query";
            return $msg;
        }
        if ($debug) {
            $fp = @fopen('sqlite.log', 'a');
            fwrite($fp, '[' . date('d.m.Y H:i') . '] [ERROR] ' . "\n\n");
            fclose($fp);
        }
        return $type_map['false'];
    }
    if ($res === true) {
        return $type_map['true'];
    }
    if (sqlite_num_rows($res) == 0) {
        if ($single) {
            return $type_map['false'];
        }
        return $type_map['true'];
    } else {
        $rows = array();
        while ($row = serendipity_db_sqlite_fetch_array($res, $type_map[$result_type])) {
            if (!empty($assocKey)) {
                // You can fetch a key-associated array via the two function parameters assocKey and assocVal
                if (empty($assocVal)) {
                    $rows[$row[$assocKey]] = $row;
                } else {
                    $rows[$row[$assocKey]] = $row[$assocVal];
                }
            } else {
                $rows[] = $row;
            }
        }
        if ($debug) {
            $fp = @fopen('sqlite.log', 'a');
            fwrite($fp, '[' . date('d.m.Y H:i') . '] SQLITE RESULT: ' . print_r($rows, true) . "\n\n");
            fclose($fp);
        }
        if ($single && count($rows) == 1) {
            return $rows[0];
        }
        return $rows;
    }
}
Ejemplo n.º 3
0
/**
 * Perform a DB Layer SQL query.
 *
 * This function returns values dependin on the input parameters and the result of the query.
 * It can return:
 *   false or a string if there was an error (depends on $expectError),
 *   true if the query succeeded but did not generate any rows
 *   array of field values if it returned a single row and $single is true
 *   array of array of field values if it returned row(s) [stacked array]
 *
 * @access public
 * @param   string      SQL query to execute
 * @param   boolean     Toggle whether the expected result is a single row (TRUE) or multiple rows (FALSE). This affects whether the returned array is 1 or 2 dimensional!
 * @param   string      Result type of the array indexing. Can be one of "assoc" (associative), "num" (numerical), "both" (numerical and associative, default)
 * @param   boolean     If true, errors will be reported. If false, errors will be ignored.
 * @param   string      A possible array key name, so that you can control the multi-dimensional mapping of an array by the key column
 * @param   string      A possible array field name, so that you can control the multi-dimensional mapping of an array by the key column and the field value.
 * @param   boolean     If true, the executed SQL error is known to fail, and should be disregarded (errors can be ignroed on DUPLICATE INDEX queries and the likes)
 * @return  mixed       Returns the result of the SQL query, depending on the input parameters
 */
function &serendipity_db_query($sql, $single = false, $result_type = "both", $reportErr = true, $assocKey = false, $assocVal = false, $expectError = false)
{
    global $serendipity;
    $type_map = array('assoc' => SQLITE3_ASSOC, 'num' => SQLITE3_NUM, 'both' => SQLITE3_BOTH, 'true' => true, 'false' => false);
    static $debug = false;
    if ($debug) {
        // Open file and write directly. In case of crashes, the pointer needs to be killed.
        $fp = @fopen('sqlite.log', 'a');
        fwrite($fp, '[' . date('d.m.Y H:i') . '] SQLITE QUERY: ' . $sql . "\n\n");
        fclose($fp);
    }
    if ($reportErr && !$expectError) {
        $res = $serendipity['dbConn']->query($sql);
    } else {
        $res = @$serendipity['dbConn']->query($sql);
    }
    if (!$res) {
        if (!$expectError && !$serendipity['production']) {
            var_dump($res);
            var_dump($sql);
            $msg = "problem with query";
            return $msg;
        }
        if ($debug) {
            $fp = @fopen('sqlite.log', 'a');
            fwrite($fp, '[' . date('d.m.Y H:i') . '] [ERROR] ' . "\n\n");
            fclose($fp);
        }
        return $type_map['false'];
    }
    if (!preg_match('@^SELECT@imsU', trim($sql))) {
        // Everything that is not SELECT will not return rows.
        // SQLite3 OO will always return an object though.
        if ($serendipity['dbConn']->lastErrorCode() > 0) {
            echo "SQLITE-ERROR: " . $serendipity['dbConn']->lastErrorMsg() . "<br />\n";
            return $type_map['false'];
        } else {
            return $type_map['true'];
        }
    }
    $rows = array();
    while ($row = serendipity_db_sqlite_fetch_array($res, $type_map[$result_type])) {
        if (!empty($assocKey)) {
            // You can fetch a key-associated array via the two function parameters assocKey and assocVal
            if (empty($assocVal)) {
                $rows[$row[$assocKey]] = $row;
            } else {
                $rows[$row[$assocKey]] = $row[$assocVal];
            }
        } else {
            $rows[] = $row;
        }
    }
    if ($debug) {
        $fp = @fopen('sqlite.log', 'a');
        fwrite($fp, '[' . date('d.m.Y H:i') . '] SQLITE RESULT: ' . print_r($rows, true) . "\n\n");
        fclose($fp);
    }
    if ($single && count($rows) == 1) {
        return $rows[0];
    }
    if (count($rows) == 0) {
        if ($single) {
            return $type_map['false'];
        }
        return $type_map['true'];
    }
    return $rows;
}