Code has been added to clipboard!
Selecting Database With MySQL
Example
<?php
echo "<table>";
echo "<tr><th>Id</th><th>Name</th><th>Surname</th></tr>";
class table_rows extends recursive_array_iterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
} Â Â Â
function current_row() {
return "<td>" . parent::current(). "</td>";
}
function begin_children() {
echo "<tr>";
}
function end_children() {
echo "</tr>" . "\n";
}
}
$server = 'server';
$user = 'user';
$pass = 'pass';
$db = 'db';
try {
$con = new PDO("mysql:host=$server;dbname=$db", $user, $pass);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//prepare the statement
$statement = $con->prepare("SELECT user_id, name, surname FROM users");
// make the query array associative
$statement->execute();
$result = $statement->setFetchMode(PDO::FETCH_ASSOC);
//iterate through the queried data
foreach(newtable_rows(newrecursive_array_iterator($stmt->fetchAll())) as $k => $v) {
echo $v;
}
} catch(PDOException $e) {
//set up an error message
echo "Error message: " . $e->getMessage();
}
$con = null;
echo "</table>";
?>