1

我使用此查询从数据库中获取数据

我如何优化和使用此连接的最佳代码?

connect();
$games_sql = mysql_query("SELECT gameid,mizbanid,mihmanid,score1,score2,gamedavar,gamestadium,now FROM games WHERE gameweek='$site_week' ORDER BY now ASC LIMIT 9");
for( $i = 0; $i < mysql_num_rows( $games_sql ); $i++ ) {
    $gameid = @mysql_result( $games_sql, $i, 0);
    $mizbanid = @mysql_result( $games_sql, $i, 1 );
    $mihmanid = @mysql_result( $games_sql, $i, 2);
    $score1 = @mysql_result( $games_sql, $i, 3);
    $score2 = @mysql_result( $games_sql, $i, 4);
    $gamedavar = @mysql_result( $games_sql, $i, 5);
    $gamestadium = @mysql_result( $games_sql, $i, 6);
    $now = @mysql_result( $games_sql, $i, 7);
    $gametimeanddate = jdate("l d M y ساعت G:i", $now);
    $gamedate = jdate("l d M y", $now);
    $gametime = jdate("G:i", $now);


`connect()` function include `mysql_connect` & `mysql_select_db`

我如何优化此代码以实现低user_connection到数据库和高速?

4

3 回答 3

0

您始终可以使用缓存和内存缓存作为优化解决方案。您可以缓存您的查询,其余的请求可以从缓存中弹出

于 2013-02-28T07:11:51.317 回答
0

为什么不查询一次数据库以获取所有信息?看看这个:

connect();
$sql = 'SELECT 
    gameid,
    mizbanid,
    mihmanid,
    score1,
    score2,
    gamedavar,
    gamestadium,
    now
FROM games
WHERE gameweek = ' . $site_week . '
ORDER BY now ASC
    LIMIT 9';
$query = mysql_query($sql);

if(mysql_num_rows($query)) {
    while($Result = mysql_fetch_object($query)) { //loop trough results

        print_r($Result); //prints the results.
        echo $Result->gameid; //this is how you echo the data
}
}

请注意,不推荐使用 mysql_query 函数,需要将其替换为 mysqli :)。

于 2013-02-28T07:13:56.570 回答
0

好吧,总有一段路要走。您可以通过多种方式进行优化,使用 php 代码以及使用查询,但是您不应该使用即将灭绝的东西

Warning

此扩展自 PHP 5.5.0 起已弃用,并将在未来删除。相反,应该使用 MySQLi 或 PDO_MySQL 扩展。另请参阅 MySQL:选择 API 指南和相关的常见问题解答以获取更多信息。此功能的替代方案包括:

mysqli_stmt_num_rows()
PDOStatement::rowCount()

如果您只想计算行数,则最好使用:-

SELECT COUNT(*) FROM SomeTable

而且你不应该使用单引号这比双引号有好处

and As mentioned by NullPointer, you should not use @ to suppress error message. Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.

于 2013-02-28T07:22:06.847 回答