note 16146 deleted from function.mysql-connect by jimw

From: Date: Sat, 08 Dec 2001 19:49:18 +0000
Subject: note 16146 deleted from function.mysql-connect by jimw
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-22128@lists.php.net to get a copy of this message
Here's an example of some odd behavior to keep your eye out for: (mysql-config.inc): $db_server = "localhost"; $db_dsn = "dsn1"; $db_user = "nobody"; $db_pass = "password"; (mysql.inc) include("mysql-config.inc"); $connectID=array(); function db_connect( $dbserver="", $dbuser="", $dbpass="" ) { global $db_server, $db_user, $db_pass, $connectID; if ( empty($dbserver) ) { $dbserver="$db_server"; } if ( empty($dbuser) ) { $dbuser="$db_user"; } if ( empty($dbpass) ) { $dbpass="$db_pass"; } if ( ! $id = mysql_connect($dbserver,$dbuser,$dbpass) ) { logToFile("Could not connect to SQL server. SQL Said: $id"); return FALSE; } else { error_log("connectID is $id",0); $connectID["$dbserver"]=$id; } } function db_disconnect( $dbserver="" ) { global $db_server, $connectID; if ( empty($dbserver) ) { $dbserver=$db_server; } $id=$connectID["$dbserver"]; if ( ! mysql_close($id) ) { logToFile("Could not disconnect from SQL server. SQL Said: " . mysql_error( )); return FALSE; } } function send_query ( $query, $dbdsn="", $dbserver="" ) { global $db_dsn, $db_server, $connectID; if ( empty($dbdsn) ) { $dbdsn=$db_dsn; } if ( empty($dbserver) ) { $dbserver=$db_server; } if ( ! mysql_select_db($dbdsn) ) { logToFile("send_query() error: could not select_db $dbdsn: " . mysql_error() ); return FALSE; } else { $result = mysql_query($query,$connectID["$dbserver"]); if ( $result == FALSE ) { logToFile("send_query() error: " . mysql_error() . " - In query: $query"); return false; } else { return $result; } } } $host1="localhost"; $host2="localhost"; // soon to be different db_connect($host1); $result1=send_query("select firstName,lastName from accounts where accountID='30'","freedomteam",$host1); db_connect($host2); $result2=send_query("select * from payments where accountID='30' AND activityID='1'","intware",$host2); $accountInfo=mysql_fetch_array($result1); while ( $paymentInfo=mysql_fetch_array($result2) ) { $total += $paymentInfo["amount"]; } db_disconnect($host2); db_disconnect($host1); What this does is generate a warning, and never disconnects. The warning is: [17-Oct-2001 15:05:38] connectID is Resource id #1 [17-Oct-2001 15:05:38] PHP Warning: Supplied argument is not a valid MySQL-Link resource in (blah)/mysql.inc on line 55. The reason for this is, when you connect to the same server twice, both connections have the same link identifier. (And the link identifier appears to be wrong when stored in hash? Not sure..) Now, what strikes me as odd is the link identifier is a string value, not integer, which would make sense if it also returns an error message, but if it's a sucessful connection couldn't it just spit out the link id# without the garbage before it? Second, on this production server I'm getting link id# 1, whereas when I connect with mysql -u nobody -ppassword intware, my link identifier displayed is in the hundreds of thousands (I don't use persistent connections as I like to have more control over disconnection from the database). Connecting to the same server twice gives the same link identifier, so when I disconnect from one database (which someday will be on a different server), I disconnect from both of them (or not at all in this case) and loose any necesary result sets. _This_is_a_documented_behavior_, I'm just warning people who might spend hours like I did wondering where the results from my query went! (and wondering if it can be changed =)

« previous php.notes (#22128) next »