gpt4 book ai didi

PHP - MySQL 到 PDO

转载 作者:搜寻专家 更新时间:2023-10-30 21:46:32 25 4
gpt4 key购买 nike

所以我决定最终转向 PDO 而不是使用旧的 mysql_

但我注意到我的网站加载速度较慢。这是一个包含 500 行的表,使用我的 mysql_ 查询它加载速度稍快(快 0.5-1 秒)。

我想知道这是否只是 PDO 的工作方式,或者我是否在某处犯了一些错误。从 MySQL 到 PDO,我没有做太多改变。

这是我原来的 mysql_ 代码:

<?php
$sql = mysql_query("SELECT * FROM rookstayers ORDER BY level DESC LIMIT 0, 500");
$id = 1;
$last_player_lvl = '';

while($row = mysql_fetch_array($sql)){
$name = $row['name'];
$level = $row['level'];
$world = $row['world'];
$account = $row['accountstatus'];
$status = $row['onlinestatus'];
$country = $row['country'];
$lastlogindate = $row['lastlogin'];
$lastlogin2 = utf8_decode($lastlogindate);
$lastlogin = str_replace("?", " ", $lastlogin2);
$onrow = '';
$typeServ = '';

$Date = $lastlogin;
$Date = substr($Date, 0, strpos($Date, " CE"));
$now = date('Y-m-d');
$datetime1 = new DateTime($Date);
$datetime2 = new DateTime($now);
$interval = $datetime1->diff($datetime2);

$difference = $interval->format('%a days ago');

$player_name = urlencode($name);

if ($status == 1){
$status = 'Online';
$onrow = 'online';
} else {
$status = 'Offline';
$onrow = 'offline';
}

if ($account == 'Premium Account'){
$account = 'Premium';
} else {
$account = 'Free';
}

if ($world == 'Aurora' || $world == 'Aurera'){
$typeServ = 'activer';
} else {
$typeServ = '';
}

echo "<tr class=" . $typeServ . ">";
echo "<td align='right'>" . ( ($last_player_lvl == $row['level']) ? '' : $id ) . "</td>";
echo "<td align='center'><img src='../img/flags/" . $country . ".gif'></td>";
echo "<td><div class='". $onrow ."'></div></td>";
echo "<td><a href='../char/" . $player_name . "' class='playerlink'>" . $name . "</a></td>";
echo "<td>" . $level . "</td>";
echo "<td><a href='../world/" . $world ."' class='worldlink'>" . $world . "</a></td>";
echo "<td>"; if ($difference == 0){ echo "Today"; } elseif($difference == 1) { echo "Yesterday"; } else { echo $difference; } echo "</td>";
echo "<td>" . $account . "</td>";
echo "</tr>";

// Check if there are duplicate levels, if so, give them the same rank
if($last_player_lvl == $row['level']){
$id = $id;
}else{
$id++;
}

$last_player_lvl = $row['level'];
}
echo "</tbody>";
echo "</table>";
?>

这是我的 PDO 代码

<?php

$sql = 'SELECT * FROM rookstayers ORDER BY level DESC LIMIT 0, 500';
$id = 1;
$last_player_lvl = '';

foreach ($db->query($sql) as $row) {
$name = $row['name'];
$level = $row['level'];
$world = $row['world'];
$account = $row['accountstatus'];
$status = $row['onlinestatus'];
$country = $row['country'];
$lastlogindate = $row['lastlogin'];
$lastlogin2 = utf8_decode($lastlogindate);
$lastlogin = str_replace("?", " ", $lastlogin2);
$onrow = '';
$typeServ = '';

$Date = $lastlogin;
$Date = substr($Date, 0, strpos($Date, " CE"));
$now = date('Y-m-d');
$datetime1 = new DateTime($Date);
$datetime2 = new DateTime($now);
$interval = $datetime1->diff($datetime2);

$difference = $interval->format('%a days ago');

$player_name = urlencode($name);

if ($status == 1){
$status = 'Online';
$onrow = 'online';
} else {
$status = 'Offline';
$onrow = 'offline';
}

if ($account == 'Premium Account'){
$account = 'Premium';
} else {
$account = 'Free';
}

if ($world == 'Aurora' || $world == 'Aurera'){
$typeServ = 'activer';
} else {
$typeServ = '';
}

echo "<tr class=" . $typeServ . ">";
echo "<td align='right'>" . ( ($last_player_lvl == $row['level']) ? '' : $id ) . "</td>";
echo "<td align='center'><img src='../img/flags/" . $country . ".gif'></td>";
echo "<td><div class='". $onrow ."'></div></td>";
echo "<td><a href='../char/" . $player_name . "' class='playerlink'>" . $name . "</a></td>";
echo "<td>" . $level . "</td>";
echo "<td><a href='../world/" . $world ."' class='worldlink'>" . $world . "</a></td>";
echo "<td>"; if ($difference == 0){ echo "Today"; } elseif($difference == 1) { echo "Yesterday"; } else { echo $difference; } echo "</td>";
echo "<td>" . $account . "</td>";
echo "</tr>";

// Check if there are duplicate levels, if so, give them the same rank
if($last_player_lvl == $row['level']){
$id = $id;
}else{
$id++;
}

$last_player_lvl = $row['level'];
}
echo "</tbody>";
echo "</table>";
?>

关于 PDO 部分,也许有什么需要改进的地方?

最佳答案

您在 foreach 循环的每次迭代中执行查询。 查看更新。

尝试替换

foreach ($db->query($sql) as $row) { ...

$result = $db->query($sql);
foreach ($result as $row) {

更新:@mario 是对的。 foreach 不会在每次迭代时评估表达式。关于为什么这会解决 OP 问题,我似乎找不到决定性的答案;我仍然认为它有一些东西,但即使在我自己的测试中,似乎使用该变量似乎对性能没有任何显着影响。如果有人要添加更多详细信息,请添加。 :)

关于PHP - MySQL 到 PDO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31258589/

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