向jquery移动站点添加数据库 [英] Adding a database to jquery mobile site

查看:89
本文介绍了向jquery移动站点添加数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用jquery和jquery mobile很陌生,但是我想知道是否有一种简单的方法(最好是教程)将数据库(远程和本地)绑定到jquery mobile站点(最好不使用php) .

I'm pretty new to using jquery and jquery mobile, but I was wondering if there was a simple way (or tutorial preferably) to tie a database (remote and local) to a jquery mobile site (preferably not using php).

推荐答案

您可以通过JavaScript使用HTML5 localStorage,这是说明和一些教程链接:

You can use HTML5 localStorage via JavaScript, here is an explanation and some links to tutorials: http://www.html5rocks.com/en/features/storage

...现在有多种技术允许应用程序将数据保存在 客户端设备...

...there are now several technologies allowing the app to save data on the client device...

如果要与服务器交互,则需要使用服务器端脚本语言.使用AJAX与服务器通信非常容易:

If you want to interact with your server then you're going to need to use a server-side scripting language. It's fairly easy to use AJAX to communicate with your server:

JS-

//run the following code whenever a new pseudo-page is created
$(document).delegate('[data-role="page"]', 'pagecreate', function () {

    //cache this page for later use (inside the AJAX function)
    var $this = $(this);

    //make an AJAX call to your PHP script
    $.getJSON('path_to/server/script.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');
    });
});

PHP-

<?php

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

//query your MySQL server for whatever information you want
$query = mysql_query("SELCT * FROM fake_table WHERE fake_col='fake value'", $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));
}
?>

您还可以将数据发送到PHP脚本并将其添加到数据库中.

You can also send data to your PHP script and have it added to a database.

以下是上面使用的功能的一些文档页面:

Here are some documentation pages for functions used above:

jQuery .getJSON(): http://api.jquery.com/jquery.getjson

PHP json_encode(): http://www.php.net/json_encode

这篇关于向jquery移动站点添加数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆