使用jQuery从MySQL获取数据 [英] fetching data from mysql with jquery

查看:431
本文介绍了使用jQuery从MySQL获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从mySQL中获取一些数据,而无需使用jQuery刷新页面.有人可以告诉我,如果mysql表中的任何记录已更新,我如何获取数据.例如,以下代码从数据库中获取一个数字计数,如果有人在数据库中添加了一个数字并进行了更新,那么如何在不刷新页面的情况下显示新数字?谢谢.

I want to fetch some data from mySQL without refreshing the page using jQuery. Can someone please tell me how I can fetch the data if any record in mysql table is updated. For example, following code fetches a number count from the database, if someone add a number in the database and it is updated, how I can display the new number without page refresh? Thanks.

<?
$query  = "SELECT number, name FROM members WHERE id=1";
$result = mysql_query($query);

$row = mysql_fetch_array($result);
$number = $row['number'];    ?>

推荐答案

它已经被提及两次了,但是我仍然想强调它.您永远不会希望客户端能够直接访问数据库.这是一个严重的安全风险.

It's been mentioned two times already, but I still want to underline it. You would never want a client to be able to access the database directly. It is a serious security risk.

现在解决.首先,您需要设置一个可以使用ajax请求的PHP文件.我们将其称为check.php,它看起来像这样:

Now to the solution. First of all you will want to set up a PHP-file that you can request with ajax. Let's call this check.php and it will look something like this:

<?php
// include necessary files to enable connection to the database
$query  = "SELECT number, name FROM members WHERE id=1";
$result = mysql_query($query);

$row = mysql_fetch_array($result);
$number = $row['number'];

// send correct content type header of json
header("Content-Type", "application/json");

// we create an array, and encode it to json
// then echo it out while killing the script
die(json_encode(array('numbers'=>$number)));

现在进入JavaScript.该解决方案与Kyle Humfeld的解决方案相似,但不会使用setInterval,因为这是一种非常不好的做法.这背后的原因是因为setInterval不会关心您的Ajax调用的状态(无论是否完成).因此,如果服务器出现问题,您可能最终会堆积请求,这不是很好.

And now onto the JavaScript. The solution is similar to that of Kyle Humfeld, but will not be using the setInterval, because it's a really bad practice. The reason behind this is because setInterval will not care about the status of your ajax call, if it has finished or not. So you might end up with stacking requests if there's something wrong with the server, and this is not good.

因此,为了避免这种情况,我们改用ajax方法的success -callback( .getJSON 本质上是 .ajax )和创建名为轮询:

So to prevent this, we instead use a combination of the success-callback of the ajax method (.getJSON is essentially a shorthand for .ajax) and setTimeout to create something that's called polling:

$(function(){
    // placeholder for request
    var request = null;

    // request function
    var check = function() {
        request = $.getJSON('check.php', function(data){
            // this will log the numbers variable to the dev console
            console.log(data.numbers);
            // initiate a timeout so the same request is done again in 5 seconds
            setTimeout(check, 5000);
        });
    });

    // initiate the request loop
    check();

    // if you want to cancel the request, just do request.abort();
});

此外,还有一种使用竞争服务器的更高级的解决方案从服务器到客户端的数据,但在挖掘彗星之前,应先尝试使以上数据起作用.如果您想阅读有关该主题的文章,建议您阅读 APE .

Also, there's a more advanced solution using a comet server that will push data from the server to the client, but you should try to get the above to work first before digging into comet. If you want to read up on the subject I suggest that you take a look at APE.

这篇关于使用jQuery从MySQL获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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