note 66105 deleted from function.pdo-construct by nlopess

From: Date: Tue, 04 Sep 2007 11:54:05 +0000
Subject: note 66105 deleted from function.pdo-construct by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-130157@lists.php.net to get a copy of this message
Note Submitter: pirasennaNSE at gmail dot com ---- 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(); } ?>

« previous php.notes (#130157) next »