doumaogui5937 2012-12-06 04:46 采纳率: 100%
浏览 43
已采纳

php从数据库获取数据会减慢页面的速度

I am using wamp server on windows. while getting a little bit of data from my database hangs my page badly. it's just like a simple post which have 1 image 1 title and a little bit discription and when I trigger the command it hangs my page badly. here is how my code looks like.

    <?php
    //1. Create a connection
    $connection= mysql_connect("localhost","root","");
    if(!$connection){
        die("Database Connection Failed :" . mysql_error());
    }

    //2 Select a database to use
    $db_select = mysql_select_db("gat", $connection);
    if (!$db_select) {
        die("Database selection failed: " . mysql_error());
    }

    ?>
<html>
<head>
    <title>Database Check</title>
</head>
<body>
    <?php 
        //3 perform database query
        $result=mysql_query("SELECT * FROM recent_works",$connection);
        if (!$result) {
            die("Database query failed:" . mysql_error());
        }

        //4 use returned data 
        while ($row= mysql_fetch_assoc($result)) {  
            echo "<div class='work_item'>";
            echo "<img src='{$row['image']}' alt=''>";
            echo "<h2>{$row['title']}</h2>";
            echo "<p>{$row['short_discription']}</p>";
            echo "</div>";
        }
     ?>
</body>
</html>
<?php 
    //5 close connection
    mysql_close($connection);
 ?>
  • 写回答

1条回答 默认 最新

  • douwen5924 2012-12-06 05:17
    关注

    Fetching data from a database will always involve some level of blocking. The question is how much data are you fetching. Your example indicates you are selecting everything from the table and fetching all of the data to print out onto the page. So how many rows are in the table, how much data is stored in each column, and how much of that data gets transferred to the client are all provisioning factors of speed here. Additionally, you have to consider that connecting to the database also has a cost.

    Here are a few suggestions I can make to the above code:

    1. Don't use the old mysql extension (mysql_* functions), but consider using the newer MySQLi extension, which can help you do things the old extension can't; like asynchronous queries. It's also highly discouraged to use the old mysql extension in new development, since it's in plans for deprecation currently. See MySQL: choosing an API in the PHP manual for more information.
    2. Check phpinfo() to make sure you aren't using output buffering (which requires buffering up to a certain amount of data before it gets sent to the client). This could result in the client waiting around until there's data ready to be sent. Pushing some HTML content out to the client as soon as possible could help improve the user experience.
    3. Don't use SELECT * FROM table in your queries, instead, consider explicitly selecting only the fields you need for each query: SELECT image,title,short_discription FROM recent_works
    4. If there's a lot of data (more than say hundred rows maybe) consider using pagination and LIMIT the query to a certain number of rows per page view. This can greatly reduce the amount of traffic between your DBMs and PHP on a per request basis.
    5. If it's a high load site consider using a persistent database connection.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?