Open kwiatchris opened 8 years ago
//SELECT WITH ERROR <?php //To Test try { $servername = ''; $username = ''; $password = ''; $database = ''; $dbport = '';
// Create connection $db = new PDO("mysql:host=$servername;dbname=$database;port=$dbport;charset=utf8", $username, $password); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $db->prepare('SELECT * FROM users WHERE userid = :userid'); $userid = filter_input(INPUT_GET, 'userid', FILTER_VALIDATE_INT);
if ($userid === false) {
echo "Error: not INPUT_GET userid";
} else {
$stmt->bindParam(':userid', $userid, PDO::PARAM_INT); // <-- Automatically sanitized for SQL by PDO
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($result);
}
} catch (PDOException $e) { echo $e->getMessage(); } ?>
<?php // PDO + MySQL // http://www.phptherightway.com/#databases //http://php.net/manual/en/pdostatement.fetchall.php $pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'root'); $statement = $pdo->query("SELECT * FROM users"); //$row = $statement->fetch(PDO::FETCH_ASSOC); //echo htmlentities($row['email']); //$result = $statement->fetchAll(); // //print_r($result); //var_dump($result); //$result = $statement->fetchAll(PDO::FETCH_ASSOC); // echo json_encode($result); // Fetch all rows, some colums echo "All rows
"; foreach ($statement->fetchAll() as $row) { echo "ID: {$row['userid']}, name: {$row['name']}
"; } // Fetching individual rows echo "First row
"; $rows = $pdo->query('SELECT * FROM users'); $firstRow = $rows->fetch(); echo "ID: {$row['userid']}, name: {$row['name']}
";