douguio0185 2015-11-07 04:40
浏览 31
已采纳

MySQL查询只循环一次

My php function only retrieves and echos one piece of information. As I added a limit of 30, it should echo 30 times with different sets of info.

The webpage I am trying to edit is available at the following: http://rocketleaguelobby.com/?key=65d9c17e957870ee15161959b2c1dedb&p=/matches

My PHP Code:

<?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:

$("#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.

Right now it only echos 1 previous bet when in the database there are multiple bets already.

  • 写回答

2条回答 默认 最新

  • dongxian5735 2015-11-07 07:08
    关注

    You did use the $result for each query. 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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向