Reputation: 135
i am facing a hight load in my server ichecked all my code and all of it is OK but what i am not sure about it is the ( multiple columns ) code.
i use this one
$getpics = mysql_query("select id,image_thumb from pics order by id desc limit 0,10");
while ($result = mysql_fetch_assoc($getpics)) {
$data1[] = $result;
}
$data1 = array_chunk($data1, 5);
echo '<table width="100%" border="0" cellpadding="5">';
foreach ($data1 as $row1) {
echo '<tr>';
foreach ($row1 as $column1) {
echo '
<td align="center">
<div class="imagepic">
'.$column1[image_thumb].'
</div>
</td>
';
}
echo '</tr>';
}
echo '</table>';
mysql_free_result($getpics);
so now i am confuced is that code make load in my site.
regards
Upvotes: 1
Views: 133
Reputation: 2049
the php itself is not going to cause any load issues, unless there is something huge going on outside of the code you've shown here.
usually your query is the bottleneck, try running explain on your query eg. EXPLAIN SELECT ...
in a tool like phpmyadmin or via the mysql command line, then post the output here.
your query is really simple though, and since you are ordering on id (primary key, auto increment I'm assuming) the issue is not likely mysql indexing.
IF the query is causing the issue, either check your indexes (could create an index on id,image_thumb) see http://dev.mysql.com/doc/refman/5.5/en/multiple-column-indexes.html , or use memcached as a temporary caching engine for your result set.
Upvotes: 0
Reputation: 82028
I took the liberty of making some of the more modifications. I removed one of the loops, got rid of array_chunk, and some others. I didn't really see too much wrong with it, so the problem may very well be in something else, but let me know.
$getpics = mysql_query("select id,image_thumb from pics order by id desc limit 0,10");
$out = '<table width="100%" border="0" cellpadding="5">'; // Use a string and concat
$i = 0;
while ($row = mysql_fetch_assoc($getpics)) { // No need to loop twice. Once is fine,
if(!($i%5))$out .= '<tr>';
$out .= '<td align="center">
<div class="imagepic">
'.$row['id']. /*Do you REALLY want ID? That's what foreach will give you*/ '
</div>
</td>
<td align="center">
<div class="imagepic">
'.$row['image_thumb'].'
</div>
</td>';
$i++;
if(!($i%5))$out .= '</tr>'; // Inner loop removed. You're only selecting two columns!
}
if(($i%5))$out .= '</tr>'; // close the last row.
echo $out.'</table>';
mysql_free_result($getpics);
Obviously check for bugs before using.
Upvotes: 1
Reputation: 1763
You can buffer the output, but the PHP script is not the blocker here. The MySQL probably shouldn't be either, since you have a LIMIT set.
Can you run DESCRIBE SELECT on that query and report the load times? Do other pages load poorly on your host as well?
Upvotes: 0
Reputation: 3178
Instead of writing out each line, you could buffer the output.
$buf = "<table width="100%" border="0" cellpadding="5">";
foreach ($data1 as $row1) {
$buf .= "<tr>";
foreach ($row1 as $column1) {
$buf .= "<td align='center'>";
$buf .= "<div class='imagepic'>";
$buf .= $column1[image_thumb];
$buf .= "</div></td>";
}
$buf .= "</tr>";
}
$buf .= "</table>";
echo $buf;
The nested for loop probably doesn't help either. Directly accessing the columns by name or index is probably going to be faster.
Upvotes: 1