2016-09-18 37 views
1

我已經使用while循環從數據庫中提取值&將它們分配給數組。但是,當循環退出時;值改變。陣列在退出循環後更改值

$i=1; 

$sql = "SELECT * FROM opportunities WHERE location = '$location'"; 
$result1 = mysqli_query($conn, $sql); 
while($row1 = mysqli_fetch_assoc($result1)) 
{ 
$job[$i]= $row1['id']; 
echo $job[$i] . "is the ID of the Job opening with this location <br>"; 

    $job[$i][1] = 3; 
    $i=$i+1; 
    echo $job['$i']; 


} 

echo $job[1]. $job[2]; 

雖然我在循環作業[「我」]返回一個不同的值到第二個實例。先謝謝你。上的評論的請求共享進一步代碼

$sq4 = "SELECT * FROM opportunities WHERE skill1 = '$skill'"; 
$result4 = mysqli_query($conn, $sq4); 
while($row4 = mysqli_fetch_assoc($result4)) 
{$id4=$row4['id']; 
echo $id4 . "is the ID of the Job opening matching the skill you entered as the most important skill<br>"; 
$job[$i]= $row4['id']; 
$job[$i][1] = 3; 
    $i=$i+1;} 

輸出:對於第一個循環,在循環(第一實例)的結果是27 & 29. 外循環,結果是23 & 23. 23是下一個循環的第一個值(如果以任何方式相關)

+0

什麼是預期和當前產出? –

+0

您不需要索引中的雙引號,但您確實需要在「作業」之前$,儘管 – RST

+0

@RST錯誤地複製了代碼。更正它。感謝您指出 –

回答

0

下面的代碼是什麼結果?

$sql  = "SELECT * FROM opportunities WHERE location = '$location'"; 
$result = mysqli_query($conn, $sql); 
$jobs = array(); 
$i  = 0; 

while ($row = mysqli_fetch_assoc($result)) 
{ 
    $jobs[$i]['id']  = $row['id']; 
    $jobs[$i]['relevance'] = 3; 

    $i++; 
} 

var_dump($jobs); 
+0

這有效,但不明白爲什麼我不會:-O –