query("SELECT * FROM brooms WHERE id=1"); if (! $rs) { $pdo->exec(<< 20, "cleansweep 7" => 30, "shooting star" => 5); foreach ($brooms as $key => $value) { $pdo->exec("INSERT INTO brooms (name, price) VALUES ('$key', '$value')"); } } else { $pdo->exec("DELETE FROM brooms WHERE name='firebolt'"); $pdo->exec("DELETE FROM brooms WHERE name='nimbus 2000'"); $pdo->exec("DELETE FROM brooms WHERE name='nimbus 2001'"); } /* * $pdo->query can be used in a foreach statement */ echo "

Retrieve all entries with \$pdo->query in a foreach

\n"; echo "\n"; foreach ($pdo->query("SELECT * FROM brooms") as $row) { echo "
${row['name']}${row['price']}\n"; } echo "
\n"; /* * INSERT using $pdo->exec executes a string as a SQL statement */ echo "

Create a new entry with \$pdo->exec

\n"; $name = "firebolt"; $price = 4000; $pdo->exec("INSERT INTO brooms (name, price) VALUES ('$name', $price)") || die("Can't add a $name broom"); echo "Inserted record for $name\n"; echo "

Retrieve \$rs->fetch(PDO::FETCH_ASSOC)

\n"; $rs = $pdo->query("SELECT * FROM brooms"); echo "\n"; while (($row = $rs->fetch(PDO::FETCH_ASSOC))) { echo "
${row['name']}${row['price']}\n"; } echo "
\n"; /* * INSERT with $pdo->prepare and bind param for list */ echo "

Create new entries with \$pdo->prepare

\n"; $brooms = array("nimbus 2000" => 100, "nimbus 2001" => 150); $stmt = $pdo->prepare("INSERT INTO brooms (name, price) VALUES (?, ?)") or die("Can't prepare statement"); $stmt->bindParam(1, $broom); $stmt->bindParam(2, $price); foreach ($brooms as $broom => $price) { $stmt->execute() || die("can't insert $broom"); echo "insert $broom
\n"; } echo "

Retrieve with PDO::FETCH_ASSOC

\n"; $rs = $pdo->query("SELECT * FROM brooms"); echo "\n"; while (($obj = $rs->fetch(PDO::FETCH_OBJ))) { echo "
$obj->name$obj->price\n"; } echo "
\n"; /* * UPDATE with transactions and $pdo->prepare */ echo "

Update entries with \$pdo->prepare

\n"; $brooms = array("nimbus 2000" => 120, "nimbus 2001" => 250); $stmt = $pdo->prepare("UPDATE brooms SET price=:price WHERE name=:broom") or die("Can't prepare statement"); $stmt->bindParam(":broom", $broom); $stmt->bindParam(":price", $price); $pdo->beginTransaction(); echo "begin transaction
\n"; foreach ($brooms as $broom => $price) { $stmt->execute() || die("can't update $broom"); echo "update $broom
\n"; } $pdo->commit(); echo "commit
\n"; echo "

Retrieve with PDO::FETCH_BOUND

\n"; $stmt = $pdo->query("SELECT name, price FROM brooms"); $stmt->bindColumn("name", $broom); $stmt->bindColumn("price", $price); echo "\n"; while ($stmt->fetch(PDO::FETCH_BOUND)) { echo "
$broom$price\n"; } echo "
\n"; /* * DELETE with $pdo->prepare */ echo "

Delete the new entries

\n"; $stmt = $pdo->prepare("DELETE FROM brooms WHERE name=:name"); $stmt->bindParam(":name", $name); foreach (array('firebolt', 'nimbus 2000', 'nimbus 2001') as $name) { $stmt->execute() || die("Can't delete $name"); echo "delete $name
\n"; } echo "

Retrieve with PDO::FETCH_OBJ

\n"; $rs = $pdo->query("SELECT * FROM brooms"); echo "\n"; while (($obj = $rs->fetch(PDO::FETCH_OBJ))) { echo "
$obj->name$obj->price\n"; } echo "
\n"; ?>