Re: db-connections and destructors (bug?)
| From: | Erik Hjortsberg | Date: | Sun, 15 Jul 2001 16:15:27 +0000 |
| Subject: | Re: db-connections and destructors (bug?) | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-792@lists.php.net to get a copy of this message | ||
At 17:43 2001-07-15 +0200, you wrote:
Erik Hjortsberg wrote: There seems to be some kind of bug with mysql-connections and the shutdown-function in php 4.0.5 (which is used by PEAR's destructor function). Apparently the connection is broken, although it is not clear how. I'm trying to do a management-system for database-objects where the changes made to the objects may be sent to the database at the end of script execution (thereby avoiding a lot of single "update..."-statements). The problem is that all queries at that time seems to abort the script without returning a Error-object. It appears as if the mysql_query() function just silently dies. Since there can be no output at script-execution end one must put debug info in the session. All of you probably know that we put a "@" in front of the php function to avoid the php error message. Sometimes it's difficult to track strange errors. I recomend the use of something like that at the top of the code: <?php error_reporting (E_ALL); // this function will handle all errors reported by PHP function php_error_handler ($errno, $errstr, $errfile, $errline) {I forgot to mention that the error doesn't have anything to do with PEAR, it in the resource allocated for mysql_connect(). A database resource allocated in the script will when used in the shutdown function silently die, without any way of knowing why it died. Test this: <code> session_start(); session_register('inScriptResult'); session_register('atEndResult'); session_register('atEndError'); $conn = mysql_connect('localhost', 'user', 'password'); mysql_select_db('database', $conn); $res = mysql_query('show tables', $conn); echo $GLOBALS['inScriptResult'] = mysql_num_rows($res); function test() {print ("In $errfile, line: $errline\n<br>$errstr");} set_error_handler ('php_error_handler'); // this function will catch errors generated by Pear, // transform it to PHP errors and trigger them to the php_error_handler function pear_error_handler ($err_obj) {$error_string =$err_obj->getMessage()."\n".$error_obj->getDebugInfo();trigger_error ($error_string, E_USER_ERROR);} require 'DB.php'; PEAR::setErrorHandling (PEAR_ERROR_CALLBACK, 'pear_error_handler'); // rest of the code ... ?> Perhaps if you try this, with luck you could get more info about the real error. Tomas V.V.Cox
$res = mysql_query('show tables', $conn);
$GLOBALS['atEndError'] = mysql_error($conn);
$GLOBALS['atEndResult'] = mysql_num_rows($res);
}
register_shutdown_function("test");
</code>
My session shows:
inScriptResult|i:27;!atEndResult|!atEndError|
i.e. the second line in test() is never executed
Now, this is nothing PEAR can fix as it's a bug/feature of php/mysql. The reason I posted it here was that PEAR supplies a mean of doing destructors through use of register_shutdown_function(). So it should be mentioned that there are some aspects you have to take in mind when writing object-destructors.
/erik hjortsberg