WHMCS MySQL and another MySQL
We have a batch processing script that performs s开发者_如何学编程omewhat these operations
- fetch data from the WHMCS database
- for each rows; if rows needs processing
- connect to data's specified external MySQL database (remote server)
- fetch and process related data
- close distant connection
- update WHMCS database
- ...
Now, while this is trivial, it breaks after the first row at step 2.4, because WHMCS does not specify a resource link for it's queries, and establishing a second mysql connection breaks the default WHMCS queries. A simple code shows what I mean :
$result = mysql_query("SELECT * FROM sometable WHERE condition");
while ($row = mysql_fetch_assoc($result)) {
$conn = mysql_connect($row['mysql_host'], $row['mysql_user'], $row['mysql_pass']);
...
mysql_close($conn);
mysql_query("UPDATE sometable SET lastrun=NOW() WHERE id={$row['id']}");
}
The second call to mysql_query
and also the one to mysql_fetch_assoc
at the second iteration will both fail.
Is there a way to "restore" the previous connection so it doesn't break? Please, do not suggest to use the $whmcsmysql
resource link everywhere, this is not an viable option as I can not modify the encrypted WHMCS source code to do so, etc.
If you're connecting to the same server every time, move the mysql_connection and mysql_close to outside of the while block, you don't need to keep connecting and disconnecting for every query.
Otherwise, open a separate connection to the WHMCS database (I don't know what WHMCS is), and specify which connection you're using in your calls to mysql_query.
$WHMCSConn = mysql_connect($user, $host, $pass);
$uniqueResultVariable = mysql_query("SELECT * FROM sometable WHERE condition", $WHMCSConn);
while ($row = mysql_fetch_assoc($uniqueResultVariable)) {
$conn = mysql_connect($row['mysql_host'], $row['mysql_user'], $row['mysql_pass']);
...
mysql_query($someQuery, $conn);
...
mysql_close($conn);
mysql_query("UPDATE sometable SET lastrun=NOW() WHERE id={$row['id']}", $WHMCSConn);
}
mysql_close($WHMCSConn);
Also, I'm sure someone is going to leave a PDO/mysqli lecture here sooner or later.
精彩评论