我已经使用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];
当我在循环作业中[" I "]返回一个不同的值给第二个实例。提前谢谢你。应注释者要求共享更多代码
$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是下一个循环的第一个值(如果它们以任何方式相关)
下面的代码会产生什么结果?
$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);