【问题标题】:Trying to combine 2 tables of data to show a list across multiple queries in PHP尝试组合 2 个数据表以显示 PHP 中多个查询的列表
【发布时间】:2022-08-15 07:56:57
【问题描述】:

我正在尝试合并 2 个数据表。但是,我所有的结果都陷入了一组结果中。我需要结果跨越多个。 这是正在发生的事情:

<?php
    $con = new mysqli(\"mysql.hostinger.co.uk\", \"-\", \"-\", \"-\");            
    $con->set_charset(\'utf8mb4\');
        
    //Retrieve all the data from the Main Data Table
    $result = $con->query(\"SELECT StreamDayID, WeekColor, DayName, StreamTitle, StreamGame, TIME_FORMAT(StartTime, \'%r\') AS StartTime, DATE_FORMAT(StreamDate, \'%D %b\') AS StreamDate FROM `WeekData` WHERE WeekColor=\'Blue\'\") or die(mysql_error());

    //Retrieve all the data from the Joiners Table
    $joinresult = $con->query(\"SELECT JoinID, JoinedBy, StreamID FROM `JoinedBy` INNER JOIN `WeekData` ON StreamID = StreamDayID\") or die(mysql_error());

    echo \"<div id=\'blue\' class=\'day\'>\";
    echo \"<h1 class=\'row\'>Blue:</h1>\";
        
    //keeps getting the next row until there are no more to get
    while($row = mysqli_fetch_array($result)){
        //Print out the contents of each row into a table
            echo \"<div id=\'blue\";
            echo $row[\'StreamDayID\'];
            echo \"\' class=\'les\'>\";
                echo \"<div class=\'title\'>\";
                    echo \"<h2>\";
                    echo $row[\'StreamTitle\'];
                    echo \"</h2>\";
                echo \"</div>\";
                echo \"<div class=\'detail\'>\";
                    echo \"<h4>Day: \";
                    echo $row[\'DayName\'];
                    echo \"</h4>\";
                    echo \"<h4>Game: \";
                    echo $row[\'StreamGame\'];
                    echo \"</h4>\";
                    echo \"<h4 class=\'joiners\'>Joined By:\";
                    while($join = mysqli_fetch_array($joinresult)){
                        echo \"<a class=\'delink\' href=\'https://twitch.tv/\";
                        echo $join[\'JoinedBy\'];
                        echo \"\'>\";
                        echo $join[\'JoinedBy\'];
                        echo \"</a>\";
                    };
                    echo \"</h4>\";
                    echo \"<h4>Start Time: \";
                    echo $row[\'StartTime\'];
                    echo \"</h4>\";
                    echo \"<h4>Stream Date: \";
                    echo $row[\'StreamDate\'];
                    echo \"</h4>\";
                echo \"</div>\";
            echo \"</div>\";
    };
?>

这是我想要发生的事情:

    标签: php html mysql


    【解决方案1】:

    似乎您的 $joinresult 需要在第一个 while 循环中定义。这将允许您过滤当前StreamID 上的JoinedBy 表。

    目前您正在返回每一个JoinedBy 中的行,因为没有过滤。

    要按StreamID 过滤,我建议使用准备好的语句并在WHERE 语句中设置过滤器:

    ...
    while($row = mysqli_fetch_array($result)){
        // initialise the statement
        $stmt = $con->stmt_init();
    
        $stmt->prepare("SELECT JoinID, JoinedBy, StreamID FROM `JoinedBy` WHERE StreamID = ?");
        // if `StreamDayID` is an integer, change "s" -> "i"
        $stmt->bind_param("s", $row["StreamDayID"]);
    
        $exec = $stmt->execute();
        if(!$exec){
            // error
            die(mysql_error());
        }
    
       $joinresult = $stmt->get_result();
    
        ...
    
        echo "<h4 class='joiners'>Joined By:";
            while($join = mysqli_fetch_array($joinresult)){
                ...
    

    注意:在这种情况下,您不必使用准备好的语句,因为您知道该值(即不太可能进行 SQL 注入),这样做是个好习惯。

    【讨论】:

    • 我是否需要更改我的 PHP 中的任何内容才能完成这项工作?直接添加时出现未定义的变量错误
    • 我不得不改变“$joinresult = $mysqli->stmt_init();”到“$joinresult = $con->stmt_init();”为了查看它显示的任何结果:警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,布尔值在 /home/u992986858/public_html/StreamSchedule/weekDataBlue.php 第 45 行给出,即“while($join = mysqli_fetch_array( $加入结果)){"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    相关资源
    最近更新 更多