在1页中简化了6个mysql查询

时间:2016-10-11 05:14:32

标签: php mysql

我有一个脚本可以在1个php页面中打开6个MySQL查询。这是剧本:

if(!isset($_SESSION["name"]) || empty($_SESSION["name"])) {
        header("Location: http://www.google");
        die();
    } else {
        $servername = "localhost";
        $username = "user";
        $password = "123";

        // Create connection
        $conn = new mysqli($servername, $username, $password);
        $conn2 = new mysqli($servername, $username, $password);
        $conn3 = new mysqli($servername, $username, $password);
        $conn4 = new mysqli($servername, $username, $password);
        $conn5 = new mysqli($servername, $username, $password);
        $conn6 = new mysqli($servername, $username, $password);

        if ($conn4->connect_error) {
            die("Connection failed: " . $conn4->connect_error);
        }
        $sql4 = "SELECT * FROM tableT WHERE date(ftdate) = curdate()"; 
        $result4 = $conn4->query($sql4);
        $row4 = $result4->fetch_assoc();
        $conn4->close();
        date_default_timezone_set("Asia/Kuala_Lumpur");

        if ($result4->num_rows < 1) {
            if ($conn3->connect_error) {
                die("Connection failed: " . $conn3->connect_error);
            }
            $date = date("Y-m-d");
            $sql3 = 'INSERT INTO tableT (`id`, `date`) VALUES (Null, "' . $date . '")';

            if ($conn3->query($sql3) === TRUE) {
                header("Location: http://google.com");
                die();
            } else {
                echo "Error: " . $sql3 . "<br>" . $conn3->error;
            }
            $conn3->close();
        } Else {
            // Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            }
            if ($conn2->connect_error) {
                die("Connection failed: " . $conn2->connect_error);
            }

            $sql = "SELECT * FROM tableB WHERE something = '0'";
            $sql2 = "SELECT * FROM tableP WHERE id=1"; 

            $result = $conn->query($sql);
            $conn->close();

            $result2 = $conn2->query($sql2);
            $row2 = $result2->fetch_assoc();
            $conn2->close();


            if ($conn5->connect_error) {
                die("Connection failed: " . $conn5->connect_error);
            }
            $month = date("m");
            $sql5 = "SELECT * FROM tableB WHERE month(date) = '" . $month . "' AND something = '1' ORDER BY fid"; 
            $result5 = $conn5->query($sql5);

            if ($result5->num_rows > 0) {
                $x = 1;
                $monthlyt = 0;

                $y = 0;
                $dailyt = 0;
                $day = date("Y-m-d");

                while($row5 = $result5->fetch_assoc()) { 
                    $monthlyt = $row5[ftotal] + $monthlyt;
                    $x = $x + 1;

                    $dbdate = date("Y-m-d", strtotime($row5[fdate]));

                    if ($dbdate == $day) { 
                    $y = $y + 1;
                    $dailyt = $row5[total] + $dailyt;
                    }
                }
                $damount = ($y / $row2[something]) * 100;
                $dtotal = ($dailyt / $row2[some]) * 100;
                $mtotal = ($monthlyt / $row2[blah]) * 100;
            }
            $conn5->close();

            if ($conn6->connect_error) {
                    die("Connection failed: " . $conn6->connect_error);
            }

            $sql6 = 'UPDATE tableT SET something="' . $y . '", some="' . $blah. '" WHERE tdate="' . $day . '"'; 

            if ($conn6->query($sql6) === TRUE) {
                //done
            } else {
                echo "Error: " . $sql6 . "<br>" . $conn6->error;
            }
            $conn6->close();
        }
    }

此页面加载一个仪表板,显示几个不同的表格和一些平均响应时间计算。

它以前工作,但我收到一条错误,说“网站重定向了你太多次”尝试清除你的cookie。今天来自chrome的ERR_TOO_MANY_REDIRECTS。

所以我在想,有没有办法可以简化这个?或者这会导致错误吗?

0 个答案:

没有答案