I just switched a old mysql_* from
a old tutorial to PDO and wanted to know if im doing it right.
I did'nt get the mysql_*
and PDO, are they drivers or just different variants to fetch data?
My code work as it should but im kinda sceptical that it work because im a beginner.
<?php
// New PDO variant
try {
$user = "user";
$pass = "";
$pdo = new PDO('mysql:host=localhost;dbname=testdb', $user, $pass);
//build query
$age = $_GET['age'];
$sex = $_GET['sex'];
$wpm = $_GET['wpm'];
$query = "SELECT * FROM ajax_example WHERE sex = '$sex'";
if(is_numeric($age))
$query .= " AND age <= $age";
if(is_numeric($wpm))
$query .= " AND wpm <= $wpm";
$stmt = $pdo->prepare($query);
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Age</th>";
$display_string .= "<th>Sex</th>";
$display_string .= "<th>WPM</th>";
$display_string .= "</tr>";
$stmt->execute(array('name' => $name));
foreach ($stmt as $row) {
$display_string .= "<tr>";
$display_string .= "<td>$row[name]</td>";
$display_string .= "<td>$row[age]</td>";
$display_string .= "<td>$row[sex]</td>";
$display_string .= "<td>$row[wpm]</td>";
$display_string .= "</tr>";
}
echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>