note 66105 added to function.pdo-construct
| From: | pirasennaNSE at gmail dot com | Date: | Mon, 15 May 2006 01:34:39 +0000 |
| Subject: | note 66105 added to function.pdo-construct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-111285@lists.php.net to get a copy of this message | ||
This code will search records of a table and print it as a HTML table.
<?php
//Connecting using PDO
try {
$dbh = new PDO('mysql:host=localhost;dbname=test',
'admin', 'password', array(PDO::ATTR_PERSISTENT => true));
// Note: if you want failures to be reflected as exception,
// set this flag.
// if this value is not set, and query fails for some reason
// it will return a boolean variable instead of the exception.
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Performing SQL query
$sql = 'SELECT * FROM tablename';
// Printing results in HTML
$results = $dbh->query($sql);
$numCols = $results->columnCount();
echo "<table border='1'>\n";
echo "\t<tr>\n";
for($i = 0; $i < $numCols; ++$i) {
$colMeta = $results->getColumnMeta($i);
echo "\t\t<td>". $colMeta['name'] ."</td>\n";
}
echo "\t</tr>\n";
// fetch returns a resultset indexed based on parameter
// you passed. You can't do
// foreach($results->fetch(PDO::FETCH_*) as $row).
$result = $results->fetch(PDO::FETCH_NUM);
do {
echo "\t<tr>\n";
for($i = 0; $i < $numCols; ++$i) {
echo "\t\t<td>$result[$i]</td>\n";
}
echo "\t</tr>\n";
} while($result = $results->fetch(PDO::FETCH_ASSOC));
echo "</table>\n";
} catch(PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
----
Server IP: 66.163.161.117
Probable Submitter: 69.227.227.118
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.pdo-construct.php
Edit -- http://master.php.net/note/edit/66105
Del: integrated -- http://master.php.net/note/delete/66105/integrated
Del: useless -- http://master.php.net/note/delete/66105/useless
Del: bad code -- http://master.php.net/note/delete/66105/bad+code
Del: spam -- http://master.php.net/note/delete/66105/spam
Del: non-english -- http://master.php.net/note/delete/66105/non-english
Del: in docs -- http://master.php.net/note/delete/66105/in+docs
Del: other reasons-- http://master.php.net/note/delete/66105
Reject -- http://master.php.net/note/reject/66105
Search -- http://master.php.net/manage/user-notes.php