javascript - 使用 PhoneGap、AJAX 和 JQuery Mobile 连接到 MySQL 数据库

标签 javascript jquery sql cordova jquery-mobile

我在一个开发 Android 应用程序的团队中,该应用程序将在很大程度上依赖于远程数据库的使用。我们正在使用 PhoneGap 和 Jquery Mobile,并一直在尝试使用 AJAX 和 JSON 调用连接到我们的 MySQL 数据库。目前,我们在测试阶段遇到了麻烦,即通过 MySQL Workbench 从 mySQL/输入中提取“Ted”的硬编码用户来验证我们是否有连接。

从我们收集到的信息来看,数据传输的过程是这样的:

  1. 在我们的 html 文件中,我们有一个

    <script type="text/javascript" src="Connect.js"></script>
    

    ^ 哪个应该运行 Connect.js 脚本,对吗?那么从那里运行 Connect.js?

  2. Connect.js 运行,将其连接到托管在外部 Web 服务上的 ServerFile.php,从而允许它运行 PHP 以连接到 MySQL 数据库并提取信息。

    //run the following code whenever a new pseudo-page is created
    $('#PAGENAME').live('pageshow', function(event)) {
    
        // cache this page for later use (inside the AJAX function)
        var $this = $(this);
    
        // make an AJAX call to your PHP script
        $.getJSON('http://www.WEBSITENAME.com/ServerFile.php', function (response) {
    
            // create a variable to hold the parsed output from the server
            var output = [];
    
            // if the PHP script returned a success
            if (response.status == 'success') {
    
                // iterate through the response rows
                for (var key in response.items) {
    
    
                     // add each response row to the output variable
                     output.push('<li>' + response.items[key] + '</li>');
                }
    
            // if the PHP script returned an error
            } else {
    
                // output an error message
                output.push('<li>No Data Found</li>');
            }
    
            // append the output to the `data-role="content"` div on this page as a
            // listview and trigger the `create` event on its parent to style the
            // listview
            $this.children('[data-role="content"]').append('<ul data-role="listview">' + output.join('') + '</ul>').trigger('create');
        });
    });
    

这是 ServerFile.php。这应该连接到 MySQL 数据库,生成 Select 语句,然后将输出发送到以 JSON 格式编码的浏览器。

<?php

//session_start();
$connection = mysql_connect("csmadison.dhcp.bsu.edu", "clbavender", "changeme"); 
$db = mysql_select_db("cs397_clbavender", $connection); 

//include your database connection code
// include_once('database-connection.php');

//query your MySQL server for whatever information you want
$query = mysql_query("SELECT * FROM Users WHERE Username ='Ted'", $db) or trigger_error(mysql_error());

//create an output array
$output = array();

//if the MySQL query returned any results
if (mysql_affected_rows() > 0) {


    //iterate through the results of your query
    while ($row = mysql_fetch_assoc($query)) {

        //add the results of your query to the output variable
        $output[] = $row;
    }


    //send your output to the browser encoded in the JSON format
    echo json_encode(array('status' => 'success', 'items' => $output));

} else {

    //if no records were found in the database then output an error message encoded in the JSON format
    echo json_encode(array('status' => 'error', 'items' => $output));
}
?>

然而这里什么也没有显示。我们从这里做什么?

最佳答案

第一件事。尝试确定问题出在哪里,是服务器端还是客户端。

打印出您的数据库查询和编码的 json 可能很有用。如果您正在创建一个简单的 API 服务,您应该能够输入 http://www.WEBSITENAME.com/ServerFile.php使用浏览器查看输出结果。

使用 echo 打印 php 的东西。

如果一切正常,是时候用 javascript 打印出您从服务器收到的响应,看看有什么问题。

使用 console.log 通过 javascript 打印内容。日志应该出现在 eclipse 的 logcat 部分(因为你正在开发 android 应用程序)

关于javascript - 使用 PhoneGap、AJAX 和 JQuery Mobile 连接到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15696440/

相关文章:

javascript - 页面刷新,javascript未运行

Javascript 创建可变深度的分层列表

sql - 使用 SQL 搜索字符串中出现次数最多的值

sql - TYPE 可以声明为 ref cursor rowtype

jquery - CSS - 如何使溢出隐藏在右边而不是左边?

java - 如何在通过 jdbc 获取数据后释放内存

javascript - 测试组件时覆盖管道

javascript - rel 属性用法是否与所有浏览器和标签兼容

javascript - YUIDoc/javascript - 如何记录模块属性

javascript - 仅在移动 View 中折叠列,仅在桌面 View 中折叠行