PHP - MySQL 到 PDO

标签 php mysql sql database pdo

所以我决定最终转向 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/

相关文章:

mysql - 如何在多行上执行 datediff

php - 触发两个表以获取主键(自动增量)到第三个链接表mysql

PHPUnit - hasOutput() 不工作

sql - 在SQL Server中,如何获取多列日期的最新日期?

mysql - 从 Select 语句中选择 : Every derived table must have its own alias

php - MySQL查询获取列名?

mysql - 将tmp表中的特定列值更新(复制)到Mysql中的主表

php - WooCommerce 从第三方 API 加载产品

php - mysqli 动态生成的表,带有打开包含行中所有值的表单的链接

javascript - 显示 JavaScript 值