1
2<?php
3$sth = $dbh->prepare("SELECT name, colour FROM fruit");
4$sth->execute();
5
6/* Exercise PDOStatement::fetch styles */
7print("PDO::FETCH_ASSOC: ");
8print("Return next row as an array indexed by column name\n");
9$result = $sth->fetch(PDO::FETCH_ASSOC);
10print_r($result);
11print("\n");
12
13print("PDO::FETCH_BOTH: ");
14print("Return next row as an array indexed by both column name and number\n");
15$result = $sth->fetch(PDO::FETCH_BOTH);
16print_r($result);
17print("\n");
18
19print("PDO::FETCH_LAZY: ");
20print("Return next row as an anonymous object with column names as properties\n");
21$result = $sth->fetch(PDO::FETCH_LAZY);
22print_r($result);
23print("\n");
24
25print("PDO::FETCH_OBJ: ");
26print("Return next row as an anonymous object with column names as properties\n");
27$result = $sth->fetch(PDO::FETCH_OBJ);
28print $result->name;
29print("\n");
30?>
31
32
1
2
3
4
5 <?php
6require_once 'dbconfig.php';
7
8try {
9 $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
10
11 $sql = 'SELECT lastname,
12 firstname,
13 jobtitle
14 FROM employees
15 ORDER BY lastname';
16
17 $q = $pdo->query($sql);
18 $q->setFetchMode(PDO::FETCH_ASSOC);
19} catch (PDOException $e) {
20 die("Could not connect to the database $dbname :" . $e->getMessage());
21}
22?>
23<!DOCTYPE html>
24<html>
25 <head>
26 <title>PHP MySQL Query Data Demo</title>
27 <link href="css/bootstrap.min.css" rel="stylesheet">
28 <link href="css/style.css" rel="stylesheet">
29 </head>
30 <body>
31 <div id="container">
32 <h1>Employees</h1>
33 <table class="table table-bordered table-condensed">
34 <thead>
35 <tr>
36 <th>First Name</th>
37 <th>Last Name</th>
38 <th>Job Title</th>
39 </tr>
40 </thead>
41 <tbody>
42 <?php while ($row = $q->fetch()): ?>
43 <tr>
44 <td><?php echo htmlspecialchars($row['lastname']) ?></td>
45 <td><?php echo htmlspecialchars($row['firstname']); ?></td>
46 <td><?php echo htmlspecialchars($row['jobtitle']); ?></td>
47 </tr>
48 <?php endwhile; ?>
49 </tbody>
50 </table>
51 </body>
52</div>
53</html>
1$pdo = new PDO('mysql:host=$host; dbname=$database;', $user, $pass);
2$stmt = $pdo->prepare('SELECT * FROM auction WHERE name = :name');
3$stmt->bindParam(':name', $_GET['searchdivebay']);
4$stmt->execute(array(':name' => $name);