Ajax调用打开了数千个mysql连接

时间:2015-04-21 11:21:35

标签: php jquery mysql ajax

我正在制作一个聊天应用程序,我必须刷新两个表。

我使用AJAX在几秒钟后刷新这些表。

但问题是它正在打开数以千计的SQL连接。

我的代码在主php页面

   <script type="text/javascript">

    $(document).ready(function(){

        refreshTable();

    });

    function refreshTable(){
        $('#table_data').load('get_table.php', function(){
            setTimeout(refreshTable, 1000);
        });
        $('#table_data_notification').load('notification_table.php', function(){
            setTimeout(refreshTable, 1000);
        });

    }
  </script>

这里是我的notification_table.php代码,将返回表的文件

<?php
session_start();
$name=$_SESSION["Username"];
?>
 <table class="table table-striped table-hover table-bordered">
  <thead>
  <tr>
    <th>Chat ID</th>
    <th>From</th>

    <th>Date</th>
    <th>Action</th>
</tr>
</thead>
<tbody>
<tr>
    <?php
    $s = 1;
    $host = "localhost";
    $user = "root";
    $pass =  "";
    $db   = "video";

    $con = mysql_connect($host,$user,$pass);
    if(!$con)
    {
        die("Could Not connect to database".mysql_error());
    }
    mysql_select_db($db,$con);

    $name=$_SESSION["Username"];
    $sql = "  SELECT * FROM chat_detail WHERE `to_user`='$name' and clicked='no' ORDER BY id DESC ";

    $getData = mysql_query($sql,$con) or die(mysql_error());
    while($fetchData = mysql_fetch_array($getData))
    {
    ?>
<tr class="gradeX" id="rowID<?php echo $s?>">
    <td><?php echo $fetchData['id']?></td>
    <td><?php echo $fetchData['from_user']?></td>
    <td ><?php echo $fetchData['chat_date']?></td>
    <td><a href="#" class="btn btn-default">Start Chat</a></td>
</tr>
<?php
$s++;
}
$con->close();
?>

</tr>

</tbody>

请建议解决方案。

修改

感谢Umair找到解决方案......我已经使用了

$(function() {
var prevAjaxReturned = true;
var xhr = null;
var xhr1 = null;
setInterval(function() {
    if( prevAjaxReturned ) {
        prevAjaxReturned = false;
    } else if( xhr ) {
        xhr.abort( );
    }
    else if( xhr1 ) {
        xhr1.abort( );
    }

    xhr = $.ajax({
        type: "GET",

        url: "get_table.php",
        success: function(html) {
            // html is a string of all output of the server script.
            $("#table_data").html(html);
            prevAjaxReturned = true;
        }

    });
    xhr1 = $.ajax({
        type: "GET",

        url: "notification_table.php",
        success: function(html) {
            // html is a string of all output of the server script.
            $("#table_data_notification").html(html);
            prevAjaxReturned = true;
        }

    });

  }, 1000);
});

并在后端mysql_pconnect();

3 个答案:

答案 0 :(得分:3)

  

但问题是它正在打开数以千计的SQL连接。

是的,当然是这样,因为每次拨打refreshTable时,它都会使用计时器自行调用两次,导致数量为2 ^ n的指数爆炸调用

不幸的是.load没有提供&#34;承诺&#34;接口,但你可以使用它:

function refreshTable() {
    $('#table_data').load('get_table.php', function() {
        $('#table_data_notification').load('notification_table.php', function(){
            setTimeout(refreshTable, 1000);
        });
    });
}

即。只有在第一次加载完成后才执行第二次加载,然后才启动计时器一次

如果你真的希望两个.load操作并行运行,你可以试试这个:

// plugin to wrap `.load` to it returns a promise
(function($) {
    $.fn.loadPromise = function(url) {
        var $this = this;
        return $.Deferred(function(def) {
            $this.load(url, def.resolve);
        }).promise();
    }
})(jQuery);

function refreshTable() {
    $.when(
        $('#table_data').loadPromise('get_table.php'),
        $('#table_data_notification').loadPromise('notification_table.php')
    ).then(function() {
        setTimeout(refreshTable, 1000);
    });
}

答案 1 :(得分:0)

除了JS嵌套调用之外,您可能需要阅读一些模式,如Singleton(http://en.wikipedia.org/wiki/Singleton_pattern)并创建一个静态类来建立与MySQL的池连接。此外,查询数据库已经有一个非常好的库idiorm。 (https://github.com/j4mie/idiorm

答案 2 :(得分:-3)

使用会做得更好的mysql_pconnect()