note 83750 added to pdostatement.fetchall

From: Date: Tue, 10 Jun 2008 12:01:56 +0000
Subject: note 83750 added to pdostatement.fetchall
Groups: php.notes 
Request: Send a blank email to php-notes+get-140301@lists.php.net to get a copy of this message
There is also another fetch mode supported on Oracle and MSSQL: PDO::FETCH_ASSOC > fetches only column names and omits the numeric index. If you would like to return all columns from an sql statement with column keys as table headers, it's as simple as this: <?php $dbh = new PDO("DS", "USERNAME", "PASSWORD"); $stmt = $dbh->prepare("SELECT * FROM tablename"); $stmt->execute(); $arrValues = $stmt->fetchAll(PDO::FETCH_ASSOC); // open the table print "<table wdith=\"100%\">\n"; print "<tr>\n"; // add the table headers foreach ($arrValues[0] as $key => $useless){ print "<th>$key</th>"; } print "</tr>"; // display data foreach ($arrValues as $row){ print "<tr>"; foreach ($row as $key => $val){ print "<td>$val</td>"; } print "</tr>\n"; } // close the table print "</table>\n"; ?> ---- Server IP: 212.124.37.9 Probable Submitter: 195.145.235.171 ---- Manual Page -- http://www.php.net/manual/en/pdostatement.fetchall.php Edit -- https://master.php.net/note/edit/83750 Del: integrated -- https://master.php.net/note/delete/83750/integrated Del: useless -- https://master.php.net/note/delete/83750/useless Del: bad code -- https://master.php.net/note/delete/83750/bad+code Del: spam -- https://master.php.net/note/delete/83750/spam Del: non-english -- https://master.php.net/note/delete/83750/non-english Del: in docs -- https://master.php.net/note/delete/83750/in+docs Del: other reasons-- https://master.php.net/note/delete/83750 Reject -- https://master.php.net/note/reject/83750 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#140301) next »