2014-01-10 48 views
0

我有以下代碼:使得從MySQL一個foreach陣列選擇

$result = mysqli_query($con, "SELECT * FROM da_questions"); 

while($row = mysqli_fetch_array($result)) 
{ 

} 

mysqli_close($con); 

從表da_questions選擇的一切。現在這是一個名爲config.php的文件,我的網站的所有頁面都需要該文件。現在我想做的事情在我的外部網頁下面......讓我們說的index.php

<?php foreach($results as $question): ?> 

    <p><?php echo $question['question_title']; ?></p> 
    <br /> 
    <p><?php echo $question['question_text']; ?></p> 
    <br /> 

<?php endforeach; ?> 

我的文件支出是如下所示:

index.php 

config/ 
    config.php 

我怎樣才能做到這一點?

+1

不知道我明白。爲什麼不只是'$ results = array(); while($ row = mysqli_fetch_array($ reult)) { $ results [] = $ row; }' –

+2

@FrankConry或者更簡單'$ results = $ result-> fetch_all(MYSQLI_ASSOC)' – Phil

+0

@FrankConry在回答中發帖?所以我可以遵守stackoverflow政策... –

回答

0

隨着你在index.php寫代碼

$result = mysqli_query($con, "SELECT * FROM da_questions"); 
    $results = array(); 
    while($row = mysqli_fetch_array($result)) 
    { 
    $results[] = $row; 
    } 

    mysqli_close($con); 

如下

include('config/config.php'); 

<?php foreach($results as $question): ?> 

    <p><?php echo $question['question_title']; ?></p> 
    <br /> 
    <p><?php echo $question['question_text']; ?></p> 
    <br /> 

    <?php endforeach; ?> 
1

爲什麼不

$results = array(); while($row = mysqli_fetch_array($reult)) { $results[] = $row; } 

或@Phil指出:

$results = $result->fetch_all(MYSQLI_ASSOC)