简体   繁体   中英

How to send/insert data in more than one Database with PhP / MySql

I have online shop where customers can order products, since now when order is complete i insert product and user data into database (server1) but now i like to insert same order data into one more database that is hosted in another hosting (server2), both machines are using php/mysql.

I think to use PhP cURL to transfer data from server1 to server2 like this

    $curl_handle=curl_init();
     $data = http_build_query($order_data);
     curl_setopt($curl_handle,CURLOPT_URL,'https://www.server2.com/orders');
     curl_setopt($curl_handle, CURLOPT_POST, 1);
     curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);
    curl_exec($curl_handle);
    curl_close($curl_handle);

My question is: will i do it with cURL or there is something better in MySql that can do it. By the way both servers are shared hosting if is matter

you could do something like

$host = ['host1','host2'];
$db_name = ['db_name1','db_name2'];
$user = ['user1','user2'];
$pass = ['pass1','pass2'];

for($i=0;$i<count($host);$i++){
   $mysqli = new mysqli($host[$i], $user[$i], $pass[$i], $db_name[$i]);
   or
   $mysqli = mysqli_connect($host[$i], $user[$i], $pass[$i], $db_name[$i]);
   And here the insert code:
   mysqli->query(....);
}

You basically initialize the mysqli php built-in function for each database and you can work with each database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM