コード例 #1
0
ファイル: db.php プロジェクト: chandradrupal/resourcespace
function sql_query($sql, $cache = false, $fetchrows = -1, $dbstruct = true)
{
    # sql_query(sql) - execute a query and return the results as an array.
    # Database functions are wrapped in this way so supporting a database server other than MySQL is
    # easier.
    # $cache is not used at this time - it was intended for disk based results caching which may be added in the future.
    # If $fetchrows is set we don't have to loop through all the returned rows. We
    # just fetch $fetchrows row but pad the array to the full result set size with empty values.
    # This has been added retroactively to support large result sets, yet a pager can work as if a full
    # result set has been returned as an array (as it was working previously).
    global $db, $querycount, $querytime, $config_show_performance_footer, $querylog, $debug_log, $mysql_verbatim_queries;
    $counter = 0;
    if ($config_show_performance_footer) {
        # Stats
        # Start measuring query time
        $time_start = microtime(true);
        $querycount++;
    }
    if ($debug_log) {
        debug("SQL: " . $sql);
    }
    # Execute query
    global $use_mysqli;
    if ($use_mysqli) {
        $result = mysqli_query($db, $sql);
    } else {
        $result = mysql_query($sql);
    }
    if ($config_show_performance_footer) {
        # Stats
        # Log performance data
        $time_end = microtime(true);
        $time_total = $time_end - $time_start;
        if (isset($querylog[$sql])) {
            $querylog[$sql]['dupe'] = $querylog[$sql]['dupe'] + 1;
            $querylog[$sql]['time'] = $querylog[$sql]['time'] + $time_total;
        } else {
            $querylog[$sql]['dupe'] = 1;
            $querylog[$sql]['time'] = $time_total;
        }
        $querytime += $time_total;
    }
    if ($use_mysqli) {
        $error = mysqli_error($db);
    } else {
        $error = mysql_error();
    }
    if ($error != "") {
        if ($error == "Server shutdown in progress") {
            echo "<span class=error>Sorry, but this query would return too many results. Please try refining your query by adding addition keywords or search parameters.<!--{$sql}--></span>";
        } elseif (substr($error, 0, 15) == "Too many tables") {
            echo "<span class=error>Sorry, but this query contained too many keywords. Please try refining your query by removing any surplus keywords or search parameters.<!--{$sql}--></span>";
        } else {
            # Check that all database tables and columns exist using the files in the 'dbstruct' folder.
            if ($dbstruct) {
                CheckDBStruct("dbstruct");
                global $plugins;
                for ($n = 0; $n < count($plugins); $n++) {
                    CheckDBStruct("plugins/" . $plugins[$n] . "/dbstruct");
                }
                # Try again (no dbstruct this time to prevent an endless loop)
                return sql_query($sql, $cache, $fetchrows, false);
                exit;
            }
            errorhandler("N/A", $error . "<br/><br/>" . $sql, "(database)", "N/A");
        }
        exit;
    } elseif ($result === true) {
        # no result set, (query was insert, update etc.)
    } else {
        $row = array();
        if ($use_mysqli) {
            while (($rs = mysqli_fetch_assoc($result)) && ($counter < $fetchrows || $fetchrows == -1)) {
                while (list($name, $value) = each($rs)) {
                    //if (!is_integer($name)) # do not run for integer values (MSSQL returns two keys for each returned column, a numeric and a text)
                    //    {
                    $row[$counter][$name] = $mysql_verbatim_queries ? $value : str_replace("\\", "", stripslashes($value));
                    //    }
                }
                $counter++;
            }
        } else {
            while (($rs = mysql_fetch_assoc($result)) && ($counter < $fetchrows || $fetchrows == -1)) {
                while (list($name, $value) = each($rs)) {
                    //if (!is_integer($name)) # do not run for integer values (MSSQL returns two keys for each returned column, a numeric and a text)
                    //    {
                    $row[$counter][$name] = $mysql_verbatim_queries ? $value : str_replace("\\", "", stripslashes($value));
                    //    }
                }
                $counter++;
            }
        }
        # If we haven't returned all the rows ($fetchrows isn't -1) then we need to fill the array so the count
        # is still correct (even though these rows won't be shown).
        $rows = count($row);
        if ($use_mysqli) {
            $totalrows = mysqli_num_rows($result);
        } else {
            $totalrows = mysql_num_rows($result);
        }
        #echo "-- $rows out of $totalrows --";
        if ($fetchrows != -1 && $rows < $totalrows) {
            $row = array_pad($row, $totalrows, 0);
        }
        return $row;
    }
}
コード例 #2
0
ファイル: db.php プロジェクト: artsmia/mia_resourcespace
function check_db_structs()
{
    CheckDBStruct("dbstruct");
    global $plugins;
    for ($n = 0; $n < count($plugins); $n++) {
        CheckDBStruct("plugins/" . $plugins[$n] . "/dbstruct");
    }
    hook("checkdbstruct");
}