By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,831 Members | 2,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,831 IT Pros & Developers. It's quick & easy.

php/mysql which is more efficient - multiple queries, or search key/value from array?

100+
P: 110
I'm building a calendar site, and am wondering if I've gone wrong in the way I have structured it.

Right now, I take the date of each cell, and pass it as a query to mysql and put the response in that cell output.
That was the easy way to do it.
But now i realize I could have selected dates between, and then used php to search the returned array for the date and then find the cell which matches that date and put the value in that cell.

The second way seems much more complicated, but I am concerned about long-term efficiency. Can anybody tell me which way is more efficient?

Here's an example of the code I have now
Expand|Select|Wrap|Line Numbers
  1.  
  2. echo"</tr><tr class=\"notes\"><td>$first_name $last_name Notes</td>";
  3.  
  4. $un=0;
  5. $newDate3 = date('y/m/d', mktime(0,0,0, $month, $day+$un, $year));
  6.  
  7. while($un<7){
  8. $queryUserNote = "SELECT notes.note FROM notes WHERE date = '$newDate3' AND uid = '$uid'";
  9. $resultUserNote = mysql_query($queryUserNote)or die(mysql_error('could not get personal notes'));
  10.  
  11. $userNoteArray=mysql_fetch_array($resultUserNote);
  12. $userNote = $userNoteArray['note'];
  13.  
  14. echo "<td class=\"notes\">$userNote</td>";
  15. $un++;
  16. }
  17.  
thanks for your help,
Pete
Feb 6 '08 #1
Share this Question
Share on Google+
3 Replies


MarkoKlacar
Expert 100+
P: 296
Hi,

My guess would be to select all the dates using a MySQL statement and then looping through the result array.

If you display a whole year you have a maximum of 365(+leap year) iterations and one SQL statement, otherwise you end up with 365(+leap year) SQL statements...

Hope I understood you correctly..

Good Luck
Feb 7 '08 #2

100+
P: 110
I happy that this maybe isn't an obvious question.

If I have about 365 queries, the majority will likely return null, so I can't just iterate through the sql response, I have to actually check that the date is in the results

eg, for each day, I have to use php to check the entire sql returned array and then it has to decide either
1 - the date is not in the array ( the result for that day was null)
2 - the date is in the array and put that data into the proper cell in the table.

I'm not sure if I clarified this or made it more confusing.
programatically, I think it is MUCH easier to query each day for data rather then querying the response array for the date and then getting the data.

Hi,

My guess would be to select all the dates using a MySQL statement and then looping through the result array.

If you display a whole year you have a maximum of 365(+leap year) iterations and one SQL statement, otherwise you end up with 365(+leap year) SQL statements...

Hope I understood you correctly..

Good Luck
Feb 8 '08 #3

MarkoKlacar
Expert 100+
P: 296
Hi,

You're probably looking at the same latency no matter what solution you choose.
Just as long as it woks.
Feb 8 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.