SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP&MySQL ed.4 redundant code?

    P.291
    The conditional statement at the bottom of the page checks to see if the email or the password passed via $_POST are empty strings. But it also checks to see if they are NULL via :
    if(!isset($_POST['email'].........
    Are the "isset" sections of the statement redundant? Even if the email and password fields are untouched before the login form is submitted, a var_dump of $_POST for email and password outputs an empty string which is what is being looked for in the the condition anyway.

    Also, on p.296
    The SQL statement here appears to include an extra join on the role table. Given that the key for role is a string value, which is also stored in the authorrole table, there appears to be no need to include the third table. I changed my code to reflect this and it works fine but I wondered if I was missing something? I appreciate that the role table would be necessary here if the primary key for role was an integer rather than a descriptive string.
    Thanks.

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by caseyc View Post
    P.291
    The conditional statement at the bottom of the page checks to see if the email or the password passed via $_POST are empty strings. But it also checks to see if they are NULL via :
    if(!isset($_POST['email'].........
    Are the "isset" sections of the statement redundant? Even if the email and password fields are untouched before the login form is submitted, a var_dump of $_POST for email and password outputs an empty string which is what is being looked for in the the condition anyway.
    It depends on your PHP error reporting level. If you have full error reporting enabled on your server, checking the value of an unset variable will cause PHP to generate a notice-level error message. To avoid this, my code first checks if the variable is set (using isset) before checking its value.

    An alternative approach is to use the error suppression operator (@):
    PHP Code:
    if (@$_POST['email'] == ''
    Using this operator is generally considered poor practice, however, since it may obscure unexpected errors and hinder your debugging efforts.

    Also, on p.296
    The SQL statement here appears to include an extra join on the role table. Given that the key for role is a string value, which is also stored in the authorrole table, there appears to be no need to include the third table. I changed my code to reflect this and it works fine but I wondered if I was missing something? I appreciate that the role table would be necessary here if the primary key for role was an integer rather than a descriptive string.
    Thanks.
    You’re absolutely right when it comes to this example. When applying this technique in general, however, the role table will often contain significant columns that you will want to include in your WHERE clause.

    I decided to join against the role table to make the sample code more useful in the general case, but it is strictly redundant for the purposes of this example, definitely.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  3. #3
    SitePoint Member
    Join Date
    May 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Appreciated,
    Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •