SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2008
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql_query() or die()

    Hi there

    I'm just thinking....why to make a die()?
    Do you know a better approach...something as writing on a log file?

    what alternatives do you use/know/recommend?....with links if is possible

  2. #2
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You normally don't want to use die. You would generally want to capture the error store it to a log file so that it can be dealt with by an administrator and display an error message to the user keeping the websites theme.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's some info about PHP error handling
    die or exit is normally just used in example code for simplicity. In your real code you'd more likely return false from a function or otherwise skip the logic that depends on that query.

    I'd turn display errors on for the development machine, but have them off and log them instead in your production environment.

    A slightly more realistic example might look like this:
    PHP Code:
    function get_name($id) {
       
    $id = (int)$id;
       if(!
    $result mysql_query("SELECT name FROM table WHERE ID = $id")) {
          return 
    false;
       }
       while(
    $r mysql_fetch_assoc($result)) {
          return 
    $r['name'];
        }
       return 
    null;
    }

    if(
    false === $name get_name(26)) {
      echo 
    'A system error occured. This error has been logged';
    }
    else if(
    $name) echo "#26 is $name";
    else echo 
    'No one exists for ID #26'

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2008
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I really need more help about this subject.

    My application has already allot of mysql_query()...or die().

    Didi you read/see/know about an alternative to my problem?

  5. #5
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not really a problem... just a question of best practices.

    Personally, I use a semi-generic custom_query function which handles errors different based on the status of the user.


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
  •