【发布时间】:2014-06-16 06:12:11
【问题描述】:
我想从数据库中获取数据并希望存储到一个数组中,在将数据存储到数组后我想访问数组的特定索引。 我是一名 java 开发人员,需要在 php 中执行此操作(我不太了解)。 基本上,表中有 250 个字符串,我想将这 250 个字符串提取到数组中,并希望访问某个特定的行。 例如:
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
**$uname100 = $row[100]; // this is not getting assigned to $uname100 variable**
**echo $row[100]; // here this is not printing**
**echo $uname100; // not even this printing**
mysqli_close($con);
?>
请检查代码中的粗体部分并帮助我。我是 php 新手,所以请不要对此感到恐慌。
也想做这样的事情:
$ctr=0;
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
$uname[$ctr++] = $row['FirstName']; // AND USING THIS OUTSIDE LOOP
}
echo $uname[90];
【问题讨论】:
-
将
print_r($row);放在while之后,看看你的数组是否包含你定义的index键?同时更新问题