0

所以我正在为数据库设计一个 Web 界面,但是当我尝试从查询中打印值时,它们会以双精度形式出现,因为它们正在计算相同值的 2 个索引。有谁知道为什么会这样?

PHP代码:

function loadTable()
{
    $name  = $_GET["table"];
    $db    = new PDO("mysql:host=localhost;dbname=university", "root", "");
    $query = "SELECT * FROM ".$name;
    $rows  = $db->query($query);

    foreach ($rows as $row)
    {
        foreach ($row as $key => $value)
        {
      print ($key.":".$value."<br>");
        }
    }
}

这是我得到的输出:

FM_ID:1234
0:1234
LAST_NAME:
1:
FIRST_NAME:
2:
OFFICE:Bliss 200
3:Bliss 200
EXTENSION:4455
4:4455
HOME_PHONE:5726952
5:5726952
MOBILE_PHONE:71283509
6:71283509
ADDRESS:
7:
EMAIL:
8:
STARTING_YEAR:2011
9:2011
TERMINATION_YEAR:2014
10:2014
LATEST_DEGREE:CMPS
11:CMPS
OBTAINED_FROM:AUB
12:AUB
DEGREE_YEAR:2014
13:2014
RESEARCH_INTEREST:Robotics
14:Robotics
4

2 回答 2

4

尝试使用

$sth = $db->prepare($sql);
$sth->execute();

$rows = $sth->fetchAll(PDO::FETCH_COLUMN);

而是使用query

于 2013-05-05T09:17:08.647 回答
4

我不确定这是否能解决你的问题,但试试这样的

$result_query = $db->query($query);
while($row = $result_query->fetch(PDO::FETCH_ASSOC)))
{

}

你可以在这里参考一些提示:http: //php.net/manual/en/pdostatement.fetch.php

于 2013-05-05T09:18:23.293 回答