简体   繁体   English

MySQL查询仅循环一次

[英]MySQL Query Only Loops Through Once

My php function only retrieves and echos one piece of information. 我的php函数仅检索和回显一条信息。 As I added a limit of 30, it should echo 30 times with different sets of info. 当我添加了30个限制时,它应使用不同的信息集回显30次。

The webpage I am trying to edit is available at the following: http://rocketleaguelobby.com/?key=65d9c17e957870ee15161959b2c1dedb&p=/matches 我要编辑的网页位于以下位置: http : //rocketleaguelobby.com/?key=65d9c17e957870ee15161959b2c1dedb&p=/matches

My PHP Code: 我的PHP代码:

<?php
// Connect to the database //
require_once($_SERVER['DOCUMENT_ROOT'].'/inc/dbconnect.php');

// Send the Query //
$query = "SELECT * FROM  `bets` WHERE `public` = '1' ORDER BY `betTime` LIMIT 0 , 30";
$result = mysqli_query($connection, $query);

if (mysqli_num_rows($result) == 0) {
    // If no results were found //
    echo '<span style="font-style: italic;opacity: 0.8;">No Bet History Found</span>';
} else {
    // Echo each bet history found //
    while($bet = mysqli_fetch_assoc($result)) {
        // For every bet, get info about the user //
        $query = "SELECT `steamName`, `steamAvatar` FROM `users` WHERE `steamID` = '".$bet['steamID']."' LIMIT 1";
        $result = mysqli_query($connection, $query);
        while ($user = mysqli_fetch_assoc($result)) {
            $query = "SELECT `teamShort` FROM `teams` WHERE `id` = '".$bet['betTeam']."' LIMIT 1";
            $result = mysqli_query($connection, $query);
            $betTeam = mysqli_fetch_assoc($result);
            $timeAgo = time() - $bet['betTime'];
            if ($timeAgo < 60) {
                // Seconds //
                if ($timeAgo >= 2) {
                    $timeAgo .= " Seconds Ago";
                } else {
                    $timeAgo .= " Second Ago";
                };
            } elseif ($timeAgo >= 60 & $timeAgo < 3600) {
                // Minutes //
                $timeAgo = round($timeAgo / 60);
                if ($timeAgo >= 2) {
                    $timeAgo .= " Minutes Ago";
                } else {
                    $timeAgo .= " Minute Ago";
                };
            } elseif ($timeAgo >= 3600 & $timeAgo < 86400) {
                // Hours //
                $timeAgo = $timeAgo / 60;
                $timeAgo = round($timeAgo / 60);
                if ($timeAgo >= 2) {
                    $timeAgo .= " Hours Ago";
                } else {
                    $timeAgo .= " Hour Ago";
                };
            } elseif ($timeAgo >= 86400 & $timeAgo < 604800) {
                // Days //
                $timeAgo = $timeAgo / 60;
                $timeAgo = $timeAgo / 60;
                $timeAgo = round($timeAgo / 24);
                if ($timeAgo >= 2) {
                    $timeAgo .= " Days Ago";
                } else {
                    $timeAgo .= " Day Ago";
                };
            } elseif ($timeAgo >= 604800 & $timeAgo < 2628000) {
                // Weeks //
                $timeAgo = $timeAgo / 60;
                $timeAgo = $timeAgo / 60;
                $timeAgo = $timeAgo / 24;
                $timeAgo = round($timeAgo / 7);
                if ($timeAgo >= 2) {
                    $timeAgo .= " Weeks Ago";
                } else {
                    $timeAgo .= " Week Ago";
                };
            } elseif ($timeAgo >= 2628000) {
                // Months //
                $timeAgo = $timeAgo / 60;
                $timeAgo = $timeAgo / 60;
                $timeAgo = $timeAgo / 24;
                $timeAgo = $timeAgo / 365;
                $timeAgo = round($timeAgo * 12);
                if ($timeAgo >= 2) {
                    $timeAgo .= " Months Ago";
                } else {
                    $timeAgo .= " Month Ago";
                };
            };
            echo '<div class="feed-post"><a href="http://steamcommunity.com/profiles/'.$bet['steamID'].'"><div class="steam-img" style="background-image: url('.$user['steamAvatar'].');"></div><span class="steam-name" title="'.$user['steamName'].'">'.$user['steamName'].'</span></a> bet <span class="bet-amount">$'.$bet['betAmount'].'</span> on <span class="bet-on">'.$betTeam['teamShort'].'</span><br><span class="bet-time">'.$timeAgo.'</span></div>';
        };
    };
};
mysqli_close($connection);
?>

My JavaScript/jQuery Code: 我的JavaScript / jQuery代码:

$("#betHistory").load('/inc/getBets.php');

I am trying to get the previous 30 bets which you should be able to see on my website link I sent. 我试图获得之前的30个投注,您应该可以在我发送的我的网站链接上看到。

Right now it only echos 1 previous bet when in the database there are multiple bets already. 现在,当数据库中已经有多个赌注时,它仅回显1个先前的赌注。

You have multiple $result = statements. 您有多个$result =语句。 The second and third statements are replacing the contents of the earlier statements. 第二和第三条语句替换了前面的语句的内容。 Change them to different names such as $result1, $result2, and $result3 and that should fix your problem. 将它们更改为不同的名称,例如$ result1,$ result2和$ result3,这样可以解决您的问题。

You can also try print_r($bet); 您也可以尝试print_r($bet); and print_r($user); print_r($user); in the appropriate parts of your program to see what is getting put in those variables. 在程序的适当部分中,查看这些变量中的内容。

You did use the $result for each query. 您确实对每个查询都使用了$ result By using a separate variable for each query you're problem will be solved. 通过为每个查询使用单独的变量,您的问题将得到解决。

I updated you're code according: 我根据以下代码更新了您的代码:

<?php
// Connect to the database //
require_once($_SERVER['DOCUMENT_ROOT'].'/inc/dbconnect.php');

// Send the Query //
$query = "SELECT * FROM  `bets` WHERE `public` = '1' ORDER BY `betTime` LIMIT 0 , 30";
$betsResult = mysqli_query($connection, $query);

if (mysqli_num_rows($betsResult) == 0) {
    // If no results were found //
    echo '<span style="font-style: italic;opacity: 0.8;">No Bet History Found</span>';
} else {
    // Echo each bet history found //
    while($bet = mysqli_fetch_assoc($betsResult)) {
        // For every bet, get info about the user //
        $query = "SELECT `steamName`, `steamAvatar` FROM `users` WHERE `steamID` = '".$bet['steamID']."' LIMIT 1";
        $usersResult = mysqli_query($connection, $query);
        while ($user = mysqli_fetch_assoc($usersResult)) {
            $query = "SELECT `teamShort` FROM `teams` WHERE `id` = '".$bet['betTeam']."' LIMIT 1";
            $teamsResult = mysqli_query($connection, $query);
            $betTeam = mysqli_fetch_assoc($teamsResult);
            $timeAgo = time() - $bet['betTime'];
            if ($timeAgo < 60) {
                // Seconds //
                if ($timeAgo >= 2) {
                    $timeAgo .= " Seconds Ago";
                } else {
                    $timeAgo .= " Second Ago";
                };
            } elseif ($timeAgo >= 60 & $timeAgo < 3600) {
                // Minutes //
                $timeAgo = round($timeAgo / 60);
                if ($timeAgo >= 2) {
                    $timeAgo .= " Minutes Ago";
                } else {
                    $timeAgo .= " Minute Ago";
                };
            } elseif ($timeAgo >= 3600 & $timeAgo < 86400) {
                // Hours //
                $timeAgo = $timeAgo / 60;
                $timeAgo = round($timeAgo / 60);
                if ($timeAgo >= 2) {
                    $timeAgo .= " Hours Ago";
                } else {
                    $timeAgo .= " Hour Ago";
                };
            } elseif ($timeAgo >= 86400 & $timeAgo < 604800) {
                // Days //
                $timeAgo = $timeAgo / 60;
                $timeAgo = $timeAgo / 60;
                $timeAgo = round($timeAgo / 24);
                if ($timeAgo >= 2) {
                    $timeAgo .= " Days Ago";
                } else {
                    $timeAgo .= " Day Ago";
                };
            } elseif ($timeAgo >= 604800 & $timeAgo < 2628000) {
                // Weeks //
                $timeAgo = $timeAgo / 60;
                $timeAgo = $timeAgo / 60;
                $timeAgo = $timeAgo / 24;
                $timeAgo = round($timeAgo / 7);
                if ($timeAgo >= 2) {
                    $timeAgo .= " Weeks Ago";
                } else {
                    $timeAgo .= " Week Ago";
                };
            } elseif ($timeAgo >= 2628000) {
                // Months //
                $timeAgo = $timeAgo / 60;
                $timeAgo = $timeAgo / 60;
                $timeAgo = $timeAgo / 24;
                $timeAgo = $timeAgo / 365;
                $timeAgo = round($timeAgo * 12);
                if ($timeAgo >= 2) {
                    $timeAgo .= " Months Ago";
                } else {
                    $timeAgo .= " Month Ago";
                };
            };
            echo '<div class="feed-post"><a href="http://steamcommunity.com/profiles/'.$bet['steamID'].'"><div class="steam-img" style="background-image: url('.$user['steamAvatar'].');"></div><span class="steam-name" title="'.$user['steamName'].'">'.$user['steamName'].'</span></a> bet <span class="bet-amount">$'.$bet['betAmount'].'</span> on <span class="bet-on">'.$betTeam['teamShort'].'</span><br><span class="bet-time">'.$timeAgo.'</span></div>';
        };
    };
};
mysqli_close($connection);
?>

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

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