2016-04-26 35 views
0

當你從前一頁走到當前頁面時,它會自動填充一個具有特定mysql查詢結果的div,唯一的問題是當頁面它最初加載它顯示以前的查詢結果,你必須刷新頁面以獲取當前結果,我不明白爲什麼。當我的頁面加載時顯示以前的mysql查詢結果不是當前的

在我的頁面頂部的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); 
} 
+0

如果你改變了返回的回聲會發生什麼? – rojobo

+0

它不工作,'返回'只是爲函數'returnDesc()' –

+0

設置一個最終值剛剛沒有設置一個值只是回聲的SQL結果,只是調用你的函數returnDesc();作爲一個空洞 – rojobo

回答

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; 
} 
+0

雖然 –

+0

這隻會顯示「數組」,你知道什麼是循環嗎? – rojobo

+0

是的,但是嵌套語句中包含的變量不能在該語句之外使用,可能爲什麼它只返回'Array'。不過謝謝你的嘗試。 –

相關問題