我有兩個領域,我知道過程妓女,但我不知道如何改善他們。如何優化這些查詢?
有問題的2個地方在這裏顯示
以及相應的查詢
區1
$nresult = mysql_query("SELECT time
FROM log
ORDER BY time
LIMIT 1") or die(mysql_error);
$nr = mysql_fetch_assoc($nresult);
$aresult = mysql_query("SELECT *
FROM log") or die(mysql_error);
$an = mysql_num_rows($aresult);
$aresult = mysql_query("SELECT AVG(players)
FROM log") or die(mysql_error);
$ap = mysql_fetch_assoc($aresult);
$average = round($an/((time()-$nr['time'])/60/60), 2);
echo "<p class=\"tf2\"><span>{$an}</span> items since " . date("F j, Y", $nr['time']) . " at " . date("g:i a", $nr['time']) . ".<br /><span>" . $average . "</span> items received per hour (<span>" . round($average/$ap['AVG(players)'], 2) . "</span>/player/hour).<br />Showing <span>{$_SESSION['limit']}</span> items per page.</p>";
區2
if (!empty($_GET['s']) && !empty($_GET['t']) && !empty($_GET['m'])&& !empty($_GET['q'])) {
if ($_GET['m'] != 'all') {
$m = "AND method = {$_GET['m']}";
}
else {
$m = '';
}
if ($_GET['q'] != 'all') {
$q = "AND quality_id = {$_GET['q']}";
}
else {
$q = '';
}
if ($_GET['s'] == ' ') {
$s = '1=1';
}
else {
$s = "{$_GET['t']} LIKE '%" . mysql_real_escape_string($_GET['s']) . "%'";
}
$num_result = mysql_query("SELECT *
FROM log
LEFT JOIN item_definition
on log.item = item_definition.item_definition_desc
LEFT JOIN method
on log.method = method.method_id
LEFT JOIN item
on item_definition.item_definition_id = item.item_id
LEFT JOIN server
on log.server = server.ip
LEFT JOIN quality
on log.quality = quality.quality_id
WHERE {$s}
{$m}
{$q}")
or die(mysql_error());
}
else {
$num_result = mysql_query("SELECT *
FROM log") or die (mysql_error());
}
$total = mysql_num_rows($num_result);
echo "<center>" . $total . " results</center><br />";
活頁是這裏http://www.tf2items.711clan.net/
正如你所看到的,它需要的時候它加載一個良好的金額。
任何提示將不勝感激。
你好[Bobby tables](http://xkcd.com/327/)。你的代碼不安全!你的應用程序很容易進行SQL注入。用'mysql_real_escape_string()'退出SQL查詢中使用的所有參數,就像你在某些地方做的一樣。不要讓這個'$ m =「和方法= {$ _GET ['m']}」;'滑過。 – Shef
首先,請添加表('log','item_definition',...)定義:字段,類型,約束,(主鍵,外鍵),索引,引擎。 –