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);
?>
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:
SELECT * FROM tablein your queries, instead, consider explicitly selecting only the fields you need for each query:SELECT image,title,short_discription FROM recent_works