上下文:我有一个大表,我试图将其分配给多个变量。我的专栏被命名为“grp”或“scr”。有 48 行,因此为了隔离每个项目,我希望能够调用变量“grp1”或“scr5”或“grp48”。
<小时/>getdata.php
$m = 1;
while ($m<=48) {
$conn->query("SELECT * matches WHERE mid = '$m'");
while($row = mysqli_fetch_array($result)) {
$grp(1-48) = $row['grp']; // Not sure how to approach this.
}
$m++;
}
<小时/>
感谢任何帮助!
最佳答案
使用数组。如果您不知道如何使用数组,请阅读有关 PHP 的书。
这是一个:http://oreilly.com/catalog/progphp/chapter/ch05.html
此外,不要运行 48 个查询,其中 1 个查询会更有效。
$conn->query("SELECT * matches WHERE mid BETWEEN 1 AND 48 ORDER BY mid");
while($row = mysqli_fetch_array($result)) {
$grp[] = $row['grp']; // add each element to the end of the $grp array
}
完成此操作后,您将拥有一个包含 48 个条目的数组 $grp
。您可以单独读取每个元素:
echo $grp[24];
关于php - 循环中的变量命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23713612/