Cann't connect to remote database using php

后端 未结 3 868
被撕碎了的回忆
被撕碎了的回忆 2021-01-28 02:17

I have Xampp installed in windows and I am creating an application using Laravel 5.3. I am trying to execute a query on another server on local network but when I try to do that

相关标签:
3条回答
  • 2021-01-28 02:31
    1. You need to grant the permission to access the database from local
    2. Use these commands and then revert , help url here grant remote access of MySQL database from any IP address
    0 讨论(0)
  • 2021-01-28 02:40

    In my own case, due to valid security concerns, i opted to develop an API to connect to the remote mysql database. I did this using php's CURL library. On the foreign domain (the domain you are connecting from), i did a curl post to the database domain (the domain holding the database).

    From here its quite easy as all you do is save the $_POST parameters using prepared statements on the local database. I just thought to put this answer here. It might help someone out there

    For example, you would like to save two values to the remote database.

    <?php
    
    $array = array('key1' => $key1, 'key2' => $key2);
    $url = 'www.site.com/api.php';    
    
    #do curl post to remote database
    $ch = curl_init();
    if ($ch !== false) {
        curl_setopt($ch, CURLOPT_URL, $url); //this is the url of the domain where the database is being hosted
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json')); //if you are returning json
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array));
        $response = curl_exec($ch);
        curl_close($ch);
        return $response;
    }
    #on the #url script, all you do is access the values via `$_POST`. example: `$_POST['key1'];`
    ?>
    
    0 讨论(0)
  • 2021-01-28 02:41

    Don't use root in password. Password field should be blank on your XAMPP set up.

    0 讨论(0)
提交回复
热议问题