1

I understand there are MANY ways to do all of this, but trying to do it the best way.

I have created the db parameters, dns, dbh, sth, sql and generally quite happy with the result up to ... well ... the result part.

<?php

// db parameters
$dbhost = "localhost";
$dbname = "x";
$dbuser = "y";
$dbpass = "z";

// driver invocation (dsn is short for data source name)
$dsn = "mysql:host=$dbhost;dbname=$dbname";

// create db object (dbh is short for database handle)
$dbh = new PDO($dsn, $dbuser, $dbpass);

// execution of database query (sth is short for statement handle)
$sql = "SELECT * FROM a_aif_remaining";
$sth = $dbh->prepare($sql);
$sth->execute();

NOT SURE WHAT TO PUT BELOW.... (A) or (B)

I just want to present a simple array of the data. One row from the table per line.

Option A

echo $_POST['fieldname1'];
echo $_POST['fieldname2'];
echo $_POST['fieldname3'];

Option B

while ($rows = $sth->fetch(PDO::FETCH_ASSOC)) {
    echo $row[fieldname1],'<br>';
    }

AND I AM CONFIDENT WITH THE ENDING

    $dbh = NULL;

?>

Any advise would be GREATLY appreciated.




UPDATED CODE: (Produces nothing on the page)

<?php

    // db parameters
    $dbhost = "localhost";
    $dbname = "theaudit_db1";
    $dbuser = "theaudit_user";
    $dbpass = "audit1999";

    $dsn = "mysql:host=$dbhost;dbname=$dbname"; // driver invocation (dsn is short for Data Source Name)

try {
    $dbh = new PDO($dsn, $dbuser, $dbpass); // connect to new db object (dbh is short for Database Handle)
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // set the PDO error mode to enable exceptions
    $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); // set the PDO emulate prepares to false

    // execute query to database (sth is short for Statement Handle)
    $sql = "SELECT * FROM a_aif_remaining";
    $sth = $dbh->prepare($sql);
    $sth->execute();

    $data = $sth->fetchAll(PDO::FETCH_ASSOC);

    $dbh = NULL;
}

catch(PDOException $e)
    {
    echo $e->getMessage();
}
?>
4

2 回答 2

1

虽然我不知道A和B之间有什么联系,但我可以回答

我只想展示一个简单的数据数组。每行从表格中提取一行。

问题。

$sql = "SELECT * FROM a_aif_remaining";
$sth = $dbh->prepare($sql);
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);

where$data是一个受欢迎的数组。

于 2012-12-28T06:14:08.993 回答
0

您更新代码的问题很简单 - 您没有将数据回显出来。你需要添加类似..

   foreach($data as $arKey=>$dataRow){
        foreach($dataRow as $arKey=>$rowField){
             echo $rowField.','; //concat with a ',' to give csv like output 
        }
        echo '<br>'; //to get to next line for each row (may want to trim the last ','
   }

我也对 $_POST 的引用感到困惑。确实两者都是关联数组,但这并不意味着 $_POST 选项是可行的 - 如果您将数据放在那里(例如 $_POST = $data),数据将仅在 $_POST 中可用,这将毫无意义。或者,如果您从其他地方发布了数据。两者似乎都不符合您的要求,因此我会忘记 $_POST 并弄清楚您如何访问多维 $data 数组。关于这个主题有无穷无尽的。尝试使用

    var_dump($data) 

看看里面有什么应该可以帮助你想象正在发生的事情。

注意:在选项 B 中,您没有正确连接或引用您的数组,它应该是:

    while ($rows = $sth->fetch(PDO::FETCH_ASSOC)) {
        echo $rows[fieldname1].'<br>'; //$row doesnt exist its $rows and you use . to concat not ,
        }

是的,可能最好使用 unset 而不是将 $dbh 设置为等于 null

unset($dbh);
于 2014-08-19T09:18:57.960 回答