简体   繁体   English

PHP的响应变得非常慢(与MySQL)

[英]php response getting very slow (with mysql)

I'm developing a dashboard, it've a notifications also so i planned for a live notifications with ajax,php,mysql.i've made it possible but the mysqli(from php) reads is slow for fetch results from database,below is my code 我正在开发一个仪表板,它也有一个通知,所以我计划使用ajax,php,mysql进行实时通知。我已经做到了,但mysqli(从php)读取的速度很慢,无法从数据库中获取结果,如下是我的代码

this is my javascript code on index.php: 这是我在index.php上的javascript代码:

comres = {
                            connect: function() {

                                    return $.ajax({
                                            url: 'read_notification.php',
                                            type: 'POST',

                                            success: function(evt, request) {
                                                     alert(evt);

                                            },
                                            complete: function() {

                                                    comres.connect();
                                            }
                                    });
                            }

                    }

                    $(document).ready(function() {

                            comres.connect();
                    });

below is my read_notification.php 以下是我的read_notification.php

<?php




 require_once("db.php");



 $query=mysqli_query($db,"select * from notifications");

while(mysqli_num_rows($query) < 1)
{
}

$query1 = mysqli_query($db,"select * from notifications");

 $row = mysqli_fetch_array($query1);

 $id=$row['id'];
  $notification=$row['notification'];



 echo "you have a notification ".$notification

mysqli_query($db,"delete from ticket.handover_response where id='$id'");


 ?>

below is working flow 下面是工作流程

  1. javascript calls the read_notification.php when loads the index.php 加载index.php时,javascript调用read_notification.php
  2. read_notification.php fire the query to database with while loop,while loop continuous still the database have any one entry on notification table read_notification.php使用while循环将查询激发到数据库,而连续循环仍然使数据库在通知表上具有任何一项
  3. when the database table have a entry the while loop get fails and the response back to the index.php 当数据库表中有一个条目时,while循环获取失败并且响应返回到index.php

here no problem is in working flow but i've a delay on after have a record on database(php is not reading suddenly from database,it's working after few more seconds), please suggest the reason or solution for this, thanks 这里的工作流程没有问题,但是在数据库上有记录后我有一个延迟(php不是突然从数据库读取,几秒钟后它就开始工作了),请提出原因或解决方案,谢谢

Try this 尝试这个

$(document).ready(function() {
setInterval(function(){
//set up an ajax request every 2 sec
   comres.connect();
},2000);
});

In your php simply display new notification based on some flag variable.that is if user click the notification set the flag to one (dont show again)otherwise mark it as zero. 在您的php中,仅显示基于某些标志变量的新通知。即,如果用户单击通知,则将标志设置为一(不再显示),否则将其标记为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM