PDA

View Full Version : How many queries is too many queries??


EquinoxWorld
07-21-2011, 12:59 PM
Hello everyone, I was wondering if someone can clarify how many queries per page is too many per script. For example my php file below. This is used to get the specific image URL for each option of the form. I know I may have been able to use some sort of array but my php knowledge is very limited still but learning fast. I wonder if this is an overkill for the server although page load time is under a second, I don't know how this many queries per file might affect when used in a live large board for example. If anyone has any input or feedback or if someone knows how I can run the same script but differently and more efficient please don't hold back. Here is the said php file;


<?php
require_once('./global.php');

define('CSRF_PROTECTION', true);

$result = $db->query_read("SELECT imgurl FROM oftw_nominations WHERE id='1'");
$row = mysql_fetch_row($result);
$id1 = $row[0];
$result = $db->query_read("SELECT imgurl FROM oftw_nominations WHERE id='2'");
$row = mysql_fetch_row($result);
$id2 = $row[0];
$result = $db->query_read("SELECT imgurl FROM oftw_nominations WHERE id='3'");
$row = mysql_fetch_row($result);
$id3 = $row[0];
$result = $db->query_read("SELECT imgurl FROM oftw_nominations WHERE id='4'");
$row = mysql_fetch_row($result);
$id4 = $row[0];
$result = $db->query_read("SELECT imgurl FROM oftw_nominations WHERE id='5'");
$row = mysql_fetch_row($result);
$id5 = $row[0];
$result = $db->query_read("SELECT imgurl FROM oftw_nominations WHERE id='6'");
$row = mysql_fetch_row($result);
$id6 = $row[0];
$result = $db->query_read("SELECT imgurl FROM oftw_nominations WHERE id='7'");
$row = mysql_fetch_row($result);
$id7 = $row[0];
echo "<div id=lightboximages class=blockbody>";
echo "<form cellpadding=10 border=1 action=oftw_insert_vote.php method=post>";
echo "<tr>";
echo "<td><center>1.<input type=radio name=votes value=1><a href=testlightbox.php?id=1 rel=Lightbox_1 id=image1><img src =".$id1." style=vertical-align:middle;padding-bottom:5px;></a></center></td>";
echo "<td><center>2.<input type=radio name=votes value=2><a href=testlightbox.php?id=2 rel=Lightbox_1 id=image2><img src =".$id2." style=vertical-align:middle;padding-bottom:5px;></a></center></td>";
echo "<td><center>3.<input type=radio name=votes value=3><a href=testlightbox.php?id=3 rel=Lightbox_1 id=image3><img src =".$id3." style=vertical-align:middle;padding-bottom:5px;></a></center></td>";
echo "<td><center>4.<input type=radio name=votes value=4><a href=testlightbox.php?id=4 rel=Lightbox_1 id=image4><img src =".$id4." style=vertical-align:middle;padding-bottom:5px;></a></center></td>";
echo "<td><center>5.<input type=radio name=votes value=5><a href=testlightbox.php?id=5 rel=Lightbox_1 id=image5><img src =".$id5." style=vertical-align:middle;padding-bottom:5px;></a></center></td>";
echo "<td><center>6.<input type=radio name=votes value=6><a href=testlightbox.php?id=6 rel=Lightbox_1 id=image6><img src =".$id6." style=vertical-align:middle;padding-bottom:5px;></a></center></td>";
echo "<td><center>7.<input type=radio name=votes value=7><a href=testlightbox.php?id=7 rel=Lightbox_1 id=image7><img src =".$id7." style=vertical-align:middle;padding-bottom:5px;></a></center></td>";
echo "</tr>";
echo "<center><input type=submit name=submitvote style=width:70px;></center>";
echo "</form>";
echo "</div>";



?>

Mooff
07-21-2011, 01:53 PM
$result = $db->query_read("
SELECT
imgurl
FROM
oftw_nominations
ORDER BY
id
ASC LIMIT 7
");

$output = '<div id=lightboximages class=blockbody>
<form cellpadding=10 border=1 action=oftw_insert_vote.php method=post>
<tr>';
foreach($result as $key => $row)
{
$output .= '<td><center>' . $key . '.<input type=radio name=votes value=' . $key . '><a href=testlightbox.php?id=' .$key. ' rel=Lightbox_1 id=image'. $key .'1><img src ='.$row.' style=vertical-align:middle;padding-bottom:5px;></a></center></td>';
};

$output .= '</tr>
<center><input type=submit name=submitvote style=width:70px;></center>
</form>
</div>';

echo "$output";


I don't know about how many querys are to many. But you can shrink them down in your code. Same goes for the echos.

sidenote: http://www.php.net/ is a great source for php functions. =)

EquinoxWorld
07-21-2011, 06:05 PM
Thanks for the reply Moof, but every time I try and use your code replacing mine I can't access the site for some reason. All pages of the site go blank.... In any case I was able to shrink the echos using your method and page load time reduced by a few milliseconds :) Every little bit helps I suppose. I wonder why I am getting those blank screens though with the rest of your code.