当我的页面加载时,它显示以前的mysql查询结果而不是当前的查询结果

时间:2016-04-26 12:29:10

标签: php mysql function echo

当你从上一页转到当前页面时,它会自动填充一个具有特定mysql查询结果的div,唯一的问题是当它最初加载的页面显示上一个查询结果时,你必须刷新获取当前结果的页面,我不明白为什么。

在我页面顶部的php中,我从另一个页面调用一个函数。这个functin检查我的数据库中的特定结果并返回它,然后将结果回显到div类中。

<--index.php-->
<?php
require_once(__DIR__.'/myFunctions.php');
$desc = returnDesc();
?>
...
...
<body>
<div class = "descBox">
    <?php echo $desc; ?>
</div>
</body>

并且函数本身就是..

<--myFunctions.php-->
function returnDesc()
{ 
    $con = connection();
    $planetMoon = (mysqli_query($con, "SELECT Sol, Star, Planet, Moon FROM MK1 WHERE id = 1"));
    if ($planetMoon->num_rows > 0) {
        while ($row = $planetMoon->fetch_assoc()) {
            $descSol = $row["Sol"];
            $descStar = $row["Star"];
            $descPlanet = $row["Planet"];
            $descMoon = $row["Moon"];
            $Desc = (mysqli_query($con, "SELECT Description FROM DescBox WHERE Sol = '$descSol' AND Star = '$descStar' AND Planet = '$descPlanet' AND Moon = '$descMoon'"));
            if ($Desc->num_rows > 0) {
                while ($row = $Desc->fetch_assoc()) {
                    return $descPlanetMoon = $row["Description"];
                }
            }
        }
    }
    mysqli_close($con);
}

1 个答案:

答案 0 :(得分:0)

更改为:

function returnDesc()
{ 
    $con = connection();
    $descPlanetMoon = array();
    $planetMoon = (mysqli_query($con, "SELECT Sol, Star, Planet, Moon FROM MK1 WHERE id = 1"));
    if ($planetMoon->num_rows > 0) {
        while ($row = $planetMoon->fetch_assoc()) {
            $descSol = $row["Sol"];
            $descStar = $row["Star"];
            $descPlanet = $row["Planet"];
            $descMoon = $row["Moon"];
            $Desc = (mysqli_query($con, "SELECT Description FROM DescBox WHERE Sol = '$descSol' AND Star = '$descStar' AND Planet = '$descPlanet' AND Moon = '$descMoon'"));
            if ($Desc->num_rows > 0) {
                while ($row = $Desc->fetch_assoc()) {
                    $descPlanetMoon[] += $row["Description"];
                }
            }
        }
    }
    mysqli_close($con);
    return $descPlanetMoon;
}