When a visitor goes to a index.php file the following code is run. There will be only one match in the DB and then it gets it’s role to include the correct page.
However, if nothing found it shows them a 404 page. I tested it and running, and I had in mind performance. In my opinion is better to have this instead of if mysql_num_rows > 0 and if { } else {}
You, with more experience than me, how do you find it ?
I gather the code above is already functioning as intended?
Contacting the database to get the user’s role will take significantly longer than almost any PHP code you might write to analyze the result that you get back, so I wouldn’t worry so much about the efficiency of the rest of your PHP here.
Only when you’re doing a long-running calculation or writing a loop that runs many, many times should you worry about efficiency more than clarity.
If this code is run 50,000+ times a second, then maybe you’re right to ask about efficiency, but if it’s a few hundred thousand times a day or less, your server will never feel the difference, and your time as a coder is much more valuable than to be used thinking about this kind of optimization.