I have a website written in PHP with a search form.
This site have lots of sections and mySql tables.
The client was complaining about the results because they were not grouped, and wanted them grouped according to the site section, like below:
Results in "About" Page:
<the results>
Results in "Blog":
<the results>
… and so on.
I had to implement a quick solution for it, so I made several queries and ran them separately… and used a foreach to iterate over the results and print them.
Well, it works, but I´m not happy about it, because it is quite slow and I wonder if I’ll have performance issues in the future.
I´m not a mysql genius myself and I just started my backend programmer carreer, so I wanted someone to give an idea of how I could handle this in a more professional way.
I was thinking of using a join, but I don’t know how I can group the results using this approach.
Any help would be very appreciated.
I really doubt a join would help you in any way. Since you said that each of the section addresses a different table, there is no way you can join them whilst still making any sense. The best that you can do is writing these queries into one sentence and then take all the needed information in one go thus saving time on the data sending
php <=> mysqlsince you will be executing once and returning once. Take a look here to see how you can do that. I really don’t think there is anything better at all that you could improve 🙂Response: Clearly, the more requests you are doing, the longer your script will perform. Have you ever tried using a command
ping google.comin cmd? You see that even you send a very small amount of data, you cannot get the response faster than 30ms or so. This is the price that you pay for any request. Plus, it also the amount of data sent adds to the time. So executing it one by one you will make many unnecessary calls. Moreover you can always try it by yourself, it is not difficult to write it in either way. And output the time spent for the task. Repeat a few times. If the time spent sending data is very insignificant, you can just leave it the easy way. But keep in mind, if your application grows bigger, every lost millisecond will add up. Multiply it by one thousand requests and you could have lost a minute of your time or even more. Anyways, definitely don’t go the UNION way because you will most likely lose the most time analyzing the data you received. EitherAnd about that function: I, myself, have never came up to need using that function so it is either me or you reading it – we would both read it from scratch. The only difference is that I knew such function existed 🙂 Weirdly enough, there is very little information on this. C# has datasets which are very good and make it easy to handle the data. PHP is slacking behind in my opinion :/ And I am all hands in C# now, so I tend to know less and less in php. If I were you, I would just copy paste the example from the link I gave and create a reusable class for later. Hope I helped at least a little bit 🙂