开发者

WHMCS MySQL and another MySQL

开发者 https://www.devze.com 2023-03-24 12:23 出处:网络
We have a batch processing script that performs s开发者_如何学编程omewhat these operations fetch data from the WHMCS database

We have a batch processing script that performs s开发者_如何学编程omewhat these operations

  1. fetch data from the WHMCS database
  2. for each rows; if rows needs processing
    1. connect to data's specified external MySQL database (remote server)
    2. fetch and process related data
    3. close distant connection
    4. update WHMCS database
  3. ...

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号