示例#1
0
<?php

$db = new pdo('sqlite::memory:');
$db->query('CREATE TABLE IF NOT EXISTS foo (id INT AUTO INCREMENT, name TEXT)');
$db->query('INSERT INTO foo VALUES (NULL, "PHP")');
$db->query('INSERT INTO foo VALUES (NULL, "PHP6")');
var_dump($db->query('SELECT * FROM foo'));
var_dump($db->errorInfo());
var_dump($db->lastInsertId());
$db->query('DROP TABLE foo');
if (isset($_POST["submit"])) {
    try {
        $db = new pdo('mysql:host=localhost;dbname=bieren', 'root', '');
        // Root password not set up
        $message = "db init";
        $brNaam = $_POST["brouwernaam"];
        $adres = $_POST["adres"];
        $postcode = $_POST["postcode"];
        $gemeente = $_POST["gemeente"];
        $omzet = $_POST["omzet"];
        $insertQuery = "INSERT INTO brouwers (brnaam, adres, postcode, gemeente, omzet) VALUES('{$brNaam}','{$adres}','{$postcode}','{$gemeente}','{$omzet}');";
        // var_dump("INSERT INTO brouwers (brnaam, adres, postcode, gemeente, omzet) VALUES(" . $brNaam . ", " . $adres . ", ". $postcode . ", " . $gemeente . ", " . $omzet . ");");
        $statement = $db->prepare($insertQuery);
        $isAdded = $statement->execute();
        if ($isAdded) {
            $id = $db->lastInsertId();
            // Returns id of the last inserted row
            $message = 'Brouwerij succesvol toegevoegd. Het unieke nummer van deze brouwerij is ' . $id . '.';
        } else {
            $message = 'Er ging iets mis met het toevoegen, probeer opnieuw';
        }
    } catch (PDOException $e) {
        $message = 'De connectie is niet gelukt.';
    }
}
?>
    
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
示例#3
0
 /**
  * 返回lastInsertId
  * @return string
  */
 public function lastInsertId()
 {
     return $this->pdo->lastInsertId();
 }