Mysql行成数组PHP

时间:2014-12-24 09:36:03

标签: php mysql arrays

这个问题已被问过很多次了,但我真的不明白怎么做。

我正在尝试将整行的值插入到PHP中的数组中。 这是查询结果:

+-----+-----+------+------+------+------+------+-----+
+ Ln1 + Ln2 + Mar1 + Mar2 + Mar3 + Mer1 + Mer2 + Mer3+
+-----+-----+------+------+------+------+------+-----+
+  1  +  1  +   1  +  1   +  1   +  1   +  1   +  1  +
+-----+-----+------+------+------+------+------+-----+

我想将所有这些'1'插入名为$giorni的数组中,实际上我正在使用

$arr_prep= array("Ln1","Ln2","Mar1","Mar2","Mar3","Mer1","Mer2","Mer3");
$giorni = array();

    for($c = 0; $c < 8; $c++){

        $temp = mysql_fetch_array(mysql_query("SELECT '$arr_prep[$c]' FROM Corsi WHERE id='$id'"));
        array_push($giorni,$temp);

    }

使用"echo "giorni -->".array_values($giorni);"打印的结果是:

giorni -->Arraygiorni -->Arraygiorni -->Arraygiorni -->Arraygiorni -->Arraygiorni -->Arraygiorni -->Arraygiorni -->Array

我也试过了mysql_fetch_arraymysql_fetch_row,也许我是以错误的方式使用它们但我无法使它工作。 谁能建议我怎么做? 谢谢

1 个答案:

答案 0 :(得分:0)

Code Example

Try Changing SELECT '$arr_prep[$c]' FROM... to SELECT $arr_prep[$c] FROM..

删除&#39;&#39;

您将获得输出 数组([0] =&gt; 1 1 =&gt; 1 [2] =&gt; 1 [3] =&gt; 1 [4] =&gt; 1 [5] =&gt; 1 [6] = &gt; 1 [7] =&gt; 1)