Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    How do I capture the mysql_error() to my log ?

    Hi,

    I usually look at the error output on my screen with this
    kind of coding:

    PHP Code:
    $result_ins mysql_query($sql_ins) or die("could not execute INSERT to cb_main."mysql_error()); 
    But I am running my script on a remote server, so I can not see
    any errors.

    What I would like to do is record them in the log file
    that I am writing as the script progresses - thats the $handle

    My code is this:
    PHP Code:
    if($new_prod == 'n' && $upd_data == 'n') {
       
    $content "No new or changed data\r\n This is Sql_ins\r\n ";
         
    fwrite($handle$content);       
            }
    else {
       
    $content "There IS new or changed data\r\n";
       
    fwrite($handle$content);       

       
    $result_ins mysql_query($sql_ins
          or die(
    "could not execute INSERT to   cb_main."mysql_error());  
         
       
    $content "Inserted to cb_main\r\n";
       
    fwrite($handle$content);          
       } 
    so instead of the die(" ") function should I put

    PHP Code:
     $result_ins mysql_query($sql_ins
       or (
    $content "could not execute INSERT to   cb_main."mysql_error());  
          
    fwrite($handle$content); ) 
    If I want the script to continue processing but record
    the error in the log, would that work ?

    I haven't used anything other than die() before so
    any advice would be appreciated



    .
    Last edited by jeddi; 02-03-2010 at 02:50 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Try the trigger_error() function
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    I have read that link,
    but am not really sure how to use it inmy script

    So, should it be like this:

    PHP Code:
    if( mysql_query($sql_ins) ) {
      
    $result_ins mysql_query($sql_ins)
     }
    else { 
       
    trigger_error  () 
       
    $content "could not execute INSERT to main."mysql_error());  
       
    fwrite($handle$content); )  
     } 
    I don't know what the point of the trigger error is
    as I will already have a real error, which is what I
    want to record.



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    The error you will have is the mysql_error. You pass that to the trigger_error
    PHP Code:
    trigger_error("could not execute INSERT to main."mysql_error()) 
    Edit: Ooops, I see you are writing it to your OWN error log, I thought you meant the php error log that you wanted to write these error messages to
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    No problem

    So probably this would work ?
    (taken the trigger out)

    PHP Code:
    if( mysql_query($sql_ins) ) {
      
    $result_ins mysql_query($sql_ins)
     }
    else { 
       
    $content "could not execute INSERT to main."mysql_error());  
       
    fwrite($handle$content); )  
     } 

    But it is a bit long...

    Can I do the same thing by replacing the die() with something ?

    something like:

    PHP Code:
    $result_ins mysql_query($sql_ins
       or { 
    $content "could not execute INSERT to   cb_main."mysql_error());  fwrite($handle$content);  } 
    ???



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Yep, create a function
    PHP Code:
    function werror($error) {
        global 
    $handle;
        
    fwrite($handle$error);

    then just use
    PHP Code:
    or werror('error message here'); 
    ie replace die with werror
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #7
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks for that,

    it is working fine.

    Just one thing:

    What if I want the process to stop after recording the error ?
    For example, I have:

    PHP Code:
    $result_ins_sub mysql_query($sql_ins_sub) or write_log("Could not INSERT INTO cb_subs .".mysql_error()."\r\n"); 
    If I want it to exit on this error, do I use the "die"
    somewhere or the php "exit;" ?

    Where would I put it in this bit of code ?

    Thanks.



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #8
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    put it in your function just after the write is done
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #9
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Ah ha, good idea.

    Just I will need two functions, one for allowing it to continue and
    one with the "exit;"

    But as ı am using them many times it is still more
    efficient to have two functions.

    thanks.



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #10
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    why not have a parameter to say wether or not to die
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)


  •  

    Posting Permissions

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