# Connect to a remote MySQL server

**URL:** <https://forums.fusetools.com/t/connect-to-a-remote-mysql-server/3692>\
**Category:** How-to Discussions\
**Created:** [February 26, 2017, 8:58am UTC](https://forums.fusetools.com/t/connect-to-a-remote-mysql-server/3692 "2017-02-26T08:58:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![GFlakes](https://avatars.discourse-cdn.com/v4/letter/g/bbe5ce/32.png) [@GFlakes](https://forums.fusetools.com/u/GFlakes)\
**Post date:** [February 26, 2017, 8:58am UTC](https://forums.fusetools.com/t/connect-to-a-remote-mysql-server/3692/1 "2017-02-26T08:58:19Z")

</div>

Hi,

I new to app development, but I have experience in C#, PHP, MySQL. I want to connect to a remote MySQL database and I have read much about that in Fuse Community I didn’t find a guide. The only thing I found is that I have to use an API to do that.

Any guide would be appreciated.

Best Gotaiba

---

<div class="post-metadata">

**Author:** ![Enrico](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/enrico/32/144_2.png) [@Enrico](https://forums.fusetools.com/u/Enrico)\
**Post date:** [February 26, 2017, 5:53pm UTC](https://forums.fusetools.com/t/connect-to-a-remote-mysql-server/3692/2 "2017-02-26T17:53:32Z")

</div>

I’m a Fuse absolute beginner too and I had to work this out by myself. To make it really simple, suppose that you are creating a web site that has to get some data from a MySQL dB but you don’t want that the web page refresh. In this case you are going to use Ajax. The most easy and common way to do Ajax calls inside a web site is with Javascript or Jquery: you connect to your MySQL via a PHP script which does all the query job and returns the result with a simple `echo`. The `echo` is then used by Javascript to update the page without refresh.

With Fuse you need to do the same thing, but instead of returning a `echo` string you need to `echo` a `json_encoded` string. So you need to create a PHP script which connects to your dB, do all the queries you want and return a `json_encoded` string. (If you need to have more control over the query you may create a script that accepts some URL parameters)

Once you have made your script, in Fuse you may call it using `Fetch`. In this [example](https://www.fusetools.com/examples/http-json) `Fetch` calls a JSON file, but you may change it with the path of your PHP script:

```
fetch('http://www.my_domain.com/myscript.php')
    .then(function(response) { 
        return response.json(); 
    })
    .then(function(responseObject) { 
        data.value = responseObject; 
    });

```

And this could be your `myscript.php`:

```
<?php
    $query_result = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
    echo json_encode($query_result);
?>
```

---

<div class="post-metadata">

**Author:** ![quincykwende](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/quincykwende/32/645_2.png) [@quincykwende](https://forums.fusetools.com/u/quincykwende)\
**Post date:** [February 27, 2017, 8:00am UTC](https://forums.fusetools.com/t/connect-to-a-remote-mysql-server/3692/3 "2017-02-27T08:00:20Z")

</div>

Great Enrico.

@GFlakes, Enrico explained how to build a basic rest-api. However, there are some guide lines to follow. This will enable you build a great RESTful APi. Please follow the guide on the link below:

> **[Best Practices for Designing a Pragmatic RESTful API](https://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api)**
>
> An API is a user interface for a developer. Learn the best practices to make an API that is easy to adopt and pleasant to use.

NB: You should note that when trying to fetch content via an API from another website, you might get an access control issue. In PHP, to solve this;

Add header(“Access-Control-Allow-Origin: \*”); at the beginning of your codes.

Regards

---

<div class="post-metadata">

**Author:** ![GFlakes](https://avatars.discourse-cdn.com/v4/letter/g/bbe5ce/32.png) [@GFlakes](https://forums.fusetools.com/u/GFlakes)\
**Post date:** [February 27, 2017, 5:05pm UTC](https://forums.fusetools.com/t/connect-to-a-remote-mysql-server/3692/4 "2017-02-27T17:05:25Z")

</div>

Thank you for the rich reply.

I’m now down to it and I hope everything works fine for me . I appreciate the great help.

---

<div class="post-metadata">

**Author:** ![olidev](https://avatars.discourse-cdn.com/v4/letter/o/e5b9ba/32.png) [@olidev](https://forums.fusetools.com/u/olidev)\
**Post date:** [July 31, 2018, 11:34am UTC](https://forums.fusetools.com/t/connect-to-a-remote-mysql-server/3692/5 "2018-07-31T11:34:00Z")

</div>

You first need to allow ip by whitelisting it on your mysql database server. After that, configure connection using following code in your connection db file

```
<?php

function getdb(){

$servername = "46.101.5.233"; // put your cloudways server IP here
$username = "qxxfumxxxbd";
$password = "xxxxbQxxmM";
$db = "qxxfumxxxbd";

try {

   $conn = mysqli_connect($servername, $username, $password, $db);

    //echo "Connected successfully";

   }

catch(exception $e)

   {

   echo "Connection failed: " . $e->getMessage();

   }

   return $conn;

}

```

Now use mysql client, like workbench to connect to database.

Source: Enabling [remote mysql connection](https://www.cloudways.com/blog/connect-to-remote-mysql-database/).
