How to connect PhoneGap App to MySql Database on the server?

后端 未结 2 2034
时光取名叫无心
时光取名叫无心 2021-01-21 04:13

I am trying to build a transit mobile application that has data stored in MySql DB on the server. Since my application is just going to connect to the MySql db and fire query/ge

相关标签:
2条回答
  • 2021-01-21 04:27

    You can use AJAX in your PhoneGap app to get data from your MySQL DB, you will need a script on your server to handle these calls. CGI/ PHP/ Ruby/ NodeJS etc... all of these will fit the job description.

    0 讨论(0)
  • 2021-01-21 04:28

    You can use AJAX to get json data from your own website. For example, this code is used to get the user's username by his id. JS on your 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 you are only using it on phonegap so no one can see your source code and play with your database :)

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