gpt4 book ai didi

php - 我如何优化mysql查询?

转载 作者:行者123 更新时间:2023-11-30 23:21:55 25 4
gpt4 key购买 nike

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

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

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 到数据库和高速?

最佳答案

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

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 替换:)。

关于php - 我如何优化mysql查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15129436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com