![]() If you try to pull that logic out into PHP, and query the database 100 times, you'll add 100x the latency and connection handling for nothing. The DB is very fast at filtering and aggregating data. Basically I get a block of text and then a linebreak. I cannot manage to create a line-break between the outputs of the loop. It does create a line-break, however only below the complete output. This is the accepted programming practice for querying and displaying multiple items from your database. I am using double-quoted strings, like I read here, as well as using HTML (because I want the browser to recognize the line-breaks too) as I read here. Then, I'm looping through the DB results with a while loop. This will automatically eliminate missing interviewers at the database level. What I'm doing above is pulling all interviewers' average times, grouping them together by interviewer #. In this case, the loop will execute forever and will never break because the variable is not updated and remains. This is a situation where you do not give any iteration in the for loop. Example 3 - Nested for loop with no iteration. We will see the break statement in detail in the coming section. $mysqli = new mysqli("localhost", "my_user", "my_password", "world") Įcho ''. Note: break statement is used to break the loop. That will kill your website's performance, when you can just use simple aggregation as illustrated below for optimal performance.Īlso, the mysql library is deprecated, use mysqli instead.ĭo this: $sql = 'SELECT interviewer, AVG(time) AS avg_time You REALLY don't want to put SQL in a loop You'll be executing 100 queries instead of a single one. I want to display it only if there is a hit. Additionally, I want that if interviewer 1 is not found in db, I don't want to display the blank row. ![]() All I want is to do this using a shorter method like using a for() loop. ![]() What actually happening is if interviewer 1 is found in db, then its average of time is to be calculated and then displayed in the table below.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |