简体   繁体   English

PDO对象行打印两次

[英]PDO object rows printing twice

so I'm designing a web interface for a database but when I try to print the values from a query they come in doubles since they are counting 2 indices for the same value. 因此,我正在设计数据库的Web界面,但是当我尝试从查询中打印值时,它们会成倍增加,因为它们会将2个索引用于同一值。 Anyone know why this is happening? 有人知道为什么会这样吗?

PHP Code: 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>");
        }
    }
}

And this is the output I get: 这是我得到的输出:

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

try using 尝试使用

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

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

instead using query 代替使用query

Im not sure if this would fix your problem but try something like this 林不知道这是否可以解决您的问题,但尝试这样的事情

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

}

you could refer for some tips here: http://php.net/manual/en/pdostatement.fetch.php 您可以在此处参考一些技巧​​: http : //php.net/manual/en/pdostatement.fetch.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM