【问题标题】:execute update query in multiple databases by ajax at same time通过ajax同时在多个数据库中执行更新查询
【发布时间】:2016-01-20 01:14:14
【问题描述】:

例如,我使用date("h:i:s")。我尝试同时对多个数据库执行更新查询,但时间可能不同 1 或 2 秒。我想知道是否有其他方法可以同时执行它..

这是我的脚本

<script type="text/javascript">
<!--
    function updateTimer() {
        var db = "1,2,",
        dbindex = db.split(","),
        countdb = 2;
        function retrieve(id) {
            if(id < countdb) {
                $.ajax({
                    url: 'update.php',
                    type: 'post',
                    data: {
                        idx : dbindex[id]-1
                    },
                    success: function (output) {
                        id++;
                        retrieve(id);
                    },
                    error: function () {
                        alert("Error Update");
                    }
                });
            }
         }
         retrieve(0);
      }
//-->
</script>

更新.php

$idx = $_POST["idx"];
$conn = $db[$index[$idx]]; // testdb and test2db
$timer = date("h:i:s");
mysqli_query($conn,"update table set timer='".$timer."'");

【问题讨论】:

    标签: php mysql ajax


    【解决方案1】:

    是的。您可以在 POST 中发送一个 DB_ids 数组;

    function retrieve(id) {
         data = [];
         for(;id<countdb;id++)
         {
              data.push(dbindex[id]-1)
         }
         $.ajax({
                 url: 'update.php',
                 type: 'post',
                 data: {
                     idx : data
                 },
                 success: function (output) {
                    alert("Ok!");
                 },
                 error: function () {
                    alert("Error Update");
                 }
             });
         }
    
    if(isset($_POST["idx"])){
        $idx = $_POST["idx"];
        foreach($idx as $id){
            $conn = $db[$index[$id]]; // testdb and test2db
            $timer = date("h:i:s");
            mysqli_query($conn,"update table set timer='".$timer."'");
        }
    }
    

    但是您需要进行一些验证。

    【讨论】:

    • 我在许多数据库中执行了插入和更新大量数据,如果我使用php循环,有时与数据库的连接会丢失。所以更新或插入的数据无法进行,我必须手动插入或更新,有什么办法吗? @andrey Sudiev
    猜你喜欢
    • 2020-11-16
    • 1970-01-01
    • 2020-06-21
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    相关资源
    最近更新 更多