vet911 Posted August 3, 2011 Share Posted August 3, 2011 I have this code below when run gets the total number of rows for each item I require. I was wondering if there is an easier way to do this or is my way okay for what I doing? It works but I thinking there might be a faster way to do this. Any suggestions will be appreciated. I thank you in advance for your help. <?php include 'config.php'; // Connect to server and select database. mysql_connect($dbhost, $dbuser, $dbpass)or die("cannot connect"); mysql_select_db("jtovey")or die("cannot select DB"); $result0 = mysql_query("SELECT * FROM $dbname WHERE ring = '1'"); $rows0 = mysql_num_rows($result0); // results $result1 = mysql_query("SELECT * FROM $dbname WHERE earrings = '1'"); $rows1 = mysql_num_rows($result1); // results $result2 = mysql_query("SELECT * FROM $dbname WHERE pendant = '1'"); $rows2 = mysql_num_rows($result2); // results $result3 = mysql_query("SELECT * FROM $dbname WHERE cab = '1'"); $rows3 = mysql_num_rows($result3); // results $result4 = mysql_query("SELECT * FROM $dbname WHERE bead = '1'"); $rows4 = mysql_num_rows($result4); $result5 = mysql_query("SELECT * FROM $dbname WHERE new = '1'"); $rows5 = mysql_num_rows($result5); $result = mysql_query("SELECT * FROM $dbname WHERE sold = '0' ORDER BY new DESC, itemno ASC" ) or die(mysql_error()); // store the record of the "" table into $row $current = ''; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { $id = $row['id']; $new = $row['new']; if (!$current) { echo "<center><div><table border='0' width='520'>"; $current = $id; echo "<img src='images/too_jewelry.png' alt='too'/><br/>"; echo "<div>"; echo "<b>Too is a direct Gemstone & Jewelry manufacturing source for the trade.</b><br/>"; echo "</div>"; echo "<div>"; echo "Questions about store items use link in menu.<br/>"; echo "Latest (".$rows5.") "; echo "Beads (".$rows4.") "; echo "Cabochons (".$rows3.") "; echo "Earrings (".$rows1.") "; echo "Pendants (".$rows2.") "; echo "Rings (".$rows0.") "; echo "</div>"; echo "<p><b>Click picture to enlarge.</b></p>"; echo "<hr width='520'>"; } elseif ($current != $id){ echo "</table></div><br><div><table border='0' width='520'>"; $current = $id; echo "<hr width='520'>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/243749-question-about-combinding-multiple-mysql_querys/ Share on other sites More sharing options...
AyKay47 Posted August 3, 2011 Share Posted August 3, 2011 use the AND clause SELECT * FROM $dbname WHERE ring=1 AND earrings = 1 AND pendant = 1 AND cab = 1 AND bead = 1 AND new = 1 AND sold = 0 ORDER BY new DESC, itemno ASC your computer will thank you Quote Link to comment https://forums.phpfreaks.com/topic/243749-question-about-combinding-multiple-mysql_querys/#findComment-1251539 Share on other sites More sharing options...
vet911 Posted August 3, 2011 Author Share Posted August 3, 2011 Thanks for the quick response. Works like a charm. Quote Link to comment https://forums.phpfreaks.com/topic/243749-question-about-combinding-multiple-mysql_querys/#findComment-1251541 Share on other sites More sharing options...
AyKay47 Posted August 3, 2011 Share Posted August 3, 2011 no problem, always look for ways to combine your queries to produce the same results...multiple queries consume more memory and slow your server, thus response time Quote Link to comment https://forums.phpfreaks.com/topic/243749-question-about-combinding-multiple-mysql_querys/#findComment-1251544 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.