...

View Full Version : How do I capture the mysql_error() to my log ?



jeddi
02-03-2010, 03:48 PM
Hi,

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


$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:


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



$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 :)



.

JAY6390
02-03-2010, 04:37 PM
Try the trigger_error() (http://www.php.net/trigger_error) function

jeddi
02-03-2010, 05:11 PM
I have read that link,
but am not really sure how to use it inmy script :(

So, should it be like this:


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.



.

JAY6390
02-03-2010, 05:17 PM
The error you will have is the mysql_error. You pass that to the trigger_error

trigger_error("could not execute INSERT to main.". mysql_error())

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

jeddi
02-03-2010, 05:38 PM
No problem :)

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


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:


$result_ins = mysql_query($sql_ins)
or { $content = "could not execute INSERT to cb_main.". mysql_error()); fwrite($handle, $content); }



???



.

JAY6390
02-03-2010, 05:42 PM
Yep, create a function

function werror($error) {
global $handle;
fwrite($handle, $error);
}then just use

or werror('error message here');ie replace die with werror

jeddi
02-06-2010, 04:26 PM
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:


$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.



.

JAY6390
02-06-2010, 04:28 PM
put it in your function just after the write is done

jeddi
02-06-2010, 08:28 PM
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.



.

JAY6390
02-06-2010, 08:32 PM
why not have a parameter to say wether or not to die



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum