SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    Smart programmer silver trophy M.Zeb Khan's Avatar
    Join Date
    Jan 2004
    Location
    Luton, Beds
    Posts
    1,792
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy 2 Errors at once

    Hello PHP guys ,

    I am newbie to PHP, just learning the basics at the time,...

    I am facing a problem, My script give 2 errors and I need only one.

    Forexample:
    PHP Code:
    mysql_query($insert) or die ("Can't add something is wrong"); 
    Notice: Undefined variable: insert in d:\sites\new\batkhela.com\khan\addit.php on line 13
    Can't add something is wrong

    The problem is it give 2 errors, (1) the Notice one and (2) the one I mentioned in the or die .......

    And I want that it display the "Can't add something" error only

    I hope someone will give me an idea

    Kinda stupid question though, haven't touched PHP before

  2. #2
    SitePoint Addict kiltman's Avatar
    Join Date
    Mar 2004
    Location
    scotland
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just change it to @mysql_query, the @ sign stops the notice error

    this should help http://uk.php.net/manual/en/language...rorcontrol.php

  3. #3
    Smart programmer silver trophy M.Zeb Khan's Avatar
    Join Date
    Jan 2004
    Location
    Luton, Beds
    Posts
    1,792
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks kitman, it worked

    And yea.. Where I can get a tutorial which will describe how to Connect/Update/Delete to mySQL database?

  4. #4
    SitePoint Addict kiltman's Avatar
    Join Date
    Mar 2004
    Location
    scotland
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.php.net/manual

    everything u could think about is there, it is the best php resource out there , you can even download it and add it into some editors

    the link for this is

    http://www.php.net/docs-echm.php

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by M.Zeb Khan

    I am newbie to PHP, just learning the basics at the time,...

    I am facing a problem, My script give 2 errors and I need only one.
    If you don't need errors, you apparently have to correct them.
    In this particular case -- define "$insert" variable.

    And do not "hide" errors with "@". Never.

  6. #6
    Smart programmer silver trophy M.Zeb Khan's Avatar
    Join Date
    Jan 2004
    Location
    Luton, Beds
    Posts
    1,792
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code I've placed isn't written by me, but from a site I think phpfreaks.com..

    so There is nothing wrong with the code I think, even PHP.NET suggest that use @ to get rid of the errors ?

  7. #7
    SitePoint Addict kiltman's Avatar
    Join Date
    Mar 2004
    Location
    scotland
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what the @ sign does is hide the notice error and just displays the die statement,

  8. #8
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Arizona, USA
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think what stereofrog is saying is that it's "best practice" to not hide errors using @, and I tend to agree (with very few exceptions), especially if you're writing code that may be used by others (i.e. libraries)..

  9. #9
    SitePoint Addict kiltman's Avatar
    Join Date
    Mar 2004
    Location
    scotland
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah the best practice is obviously to deal with errors instead of hiding them, nut i think for what is needed the @ sign does the job, this is the method i personally would use for the specific above code, if there wasnt a die statement after it then i wouldnt use the @ as it would would not tell me what was wrong

  10. #10
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Arizona, USA
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kiltman
    yeah the best practice is obviously to deal with errors instead of hiding them, nut i think for what is needed the @ sign does the job, this is the method i personally would use for the specific above code, if there wasnt a die statement after it then i wouldnt use the @ as it would would not tell me what was wrong
    I guess we'll just have to agree to disagree regarding the above code.. Sure, you'll see that the query attempt dies, but why? Was the query malformed? Is the database connection suddenly unavailable? Or is it simply because your query variable ($insert) is not initalized (for whatever reason)?

    You get much more debugging information regarding the source of the problem, IMO, by not supressing the NOTICE.

  11. #11
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    if it's your first jump in the world of PHP/MySql, I strongly suggest that you read Kevin Yank's book released by SitePoint: Build your own Databse Driven Website.

    There are some sample chapters that you can check in the article section of site point

  12. #12
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bdunlap
    I guess we'll just have to agree to disagree regarding the above code.. Sure, you'll see that the query attempt dies, but why? Was the query malformed? Is the database connection suddenly unavailable? Or is it simply because your query variable ($insert) is not initalized (for whatever reason)?

    You get much more debugging information regarding the source of the problem, IMO, by not supressing the NOTICE.
    True

    As long as you develop, get as much info as you can. But once the project is alive, maybe that your users would not be very happy to see the error message generated automatically.

    So add the @ and manage your errors yourself, by redirecting your users to a page where they will be informed of the problem:
    PHP Code:
    else

    {

    header('Location: error.php?database=connectionfailed');



  13. #13
    SitePoint Addict kiltman's Avatar
    Join Date
    Mar 2004
    Location
    scotland
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    duuudie what u said was what i meant in my head, just explained it wrong to close to new year

  14. #14
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Arizona, USA
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by duuudie
    As long as you develop, get as much info as you can. But once the project is alive, maybe that your users would not be very happy to see the error message generated automatically.
    Absolutely. I hope it was obvious that I wasn't advocating the display of E_NOTICE (or any raw PHP error, for that matter) messages on a production system to end users.

    Quote Originally Posted by duuudie
    So add the @ and manage your errors yourself, by redirecting your users to a page where they will be informed of the problem:
    This I don't agree with. You'd be much better served by simply adding the following to the top of whatever "bootstrap" script will be using the above code (or change the setting in your php.ini, if you have access to it):

    PHP Code:
    ini_set('display_errors'FALSE); 
    Much better practice than going through and adding '@' to every call that you want to suppress errors on, IMO..

  15. #15
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bdunlap

    100% agree. Even better would be to setup your own error handler (via set_error_handler in php4 or exceptions in 5)... Actually, it's a must for every serious project, imho.

    But all this is pretty advanced stuff, for the people who "just learn the basics" like OP does, it's enough to have error_reporting(E_ALL) and dont use '@' .

  16. #16
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Arizona, USA
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    Even better would be to setup your own error handler (via set_error_handler in php4 or exceptions in 5)... Actually, it's a must for every serious project, imho.
    I agree with you 100% . Didn't want to go down the set_error_handler() road though. As you said, probably overkill for what OP was looking for.

    I just wanted to make the point (as you did) that arbitrary use of @ to suppress errors is not best practice and there are much better ways to handle such things. Even those that are "learning the basics" would be best served by avoiding such habits, IMO..


  17. #17
    Smart programmer silver trophy M.Zeb Khan's Avatar
    Join Date
    Jan 2004
    Location
    Luton, Beds
    Posts
    1,792
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you guys, Getting replies really quick

    Quote Originally Posted by duuudie
    if it's your first jump in the world of PHP/MySql, I strongly suggest that you read Kevin Yank's book released by SitePoint: Build your own Databse Driven Website.
    I love that book, but SitePoint doesn't send books to pakistan any guy overhere have that book? I will pay him/her to send me that book to pakistan..

    I learn easily from books instead of eBooks or online totorials..

  18. #18
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bdublap: I totally agree. However, since he was using the @ sign, I just thought that I wouldn't confuse him with some thoughts on configuration options

    But you're right to advokate best practice

  19. #19
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,479
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Cool

    Quote Originally Posted by M.Zeb Khan
    I love that book, but SitePoint doesn't send books to pakistan any guy overhere have that book? I will pay him/her to send me that book to pakistan..

    I learn easily from books instead of eBooks or online totorials..
    If you can't get/have that book, then you should try & read something that was the starting point of that book, the tutorial series of "Build your own Database driven website using PHP & MySQL". Its pretty cool & you'll get the hang of using PHP & MySQL pretty quick. I got it in just 10 minutes of reading that tutorial.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!


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
  •