在循环中命名变量

时间:2014-05-17 16:25:46

标签: php mysql

上下文:我有一个大表,我试图分配给几个变量。我的专栏名为" 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++;
}

感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

使用数组。如果您不知道如何使用数组,请阅读有关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];