How to connect PhoneGap application to MySql database on the server?

I am trying to create a transit mobile application with data stored in MySql DB on the server. Since my application is just about to connect to MySql db and request fire / get data and show it to the user. I planned to create it using PhoneGap, but I don’t understand how to connect to MySql DB if the phone saver allows local data storage using SqlLite? Can someone inform me about the architecture that I have to consider in order to create this transit application.

+3
source share
2 answers

You can use AJAX in your PhoneGap application to get data from your MySQL database, to process these calls you will need a script on your server. CGI / PHP / Ruby / NodeJS etc. All of them will correspond to the job description.

+4
source

You can use AJAX to get JSON data from your own site. For example, this code is used to get the username of a user by its identifier. JS in PhoneGap app:

function getUsername(){
    var id = document.getElementById("textbox").value;
    $.ajax({
        url: "https://www.example.com/json_read?id=" + id,
        type: 'GET',
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            alert(data.username);
        },
        error: function () {
            alert("Error");
        }
    });
}

PHP on your website:

<?php
require 'db.php';
$id = $_GET['id'];
$sql = $mysqli->query("SELECT * FROM users WHERE id='$id'");
$result = $sql->fetch_assoc();
$object->username = $result['username'];
$json = json_encode($object);

echo $json;
?>

Make sure that you use it only in PhoneGap so that no one can see your source code and play with your database :)

0
source

All Articles