-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path85. PDOPrepSelectWithWhere1.php
More file actions
49 lines (36 loc) · 1.14 KB
/
85. PDOPrepSelectWithWhere1.php
File metadata and controls
49 lines (36 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
// create variable for connection
$dsn = "mysql:host=localhost; dbname=test_db";
$db_user = "root";
$db_password = "";
// Create Connection with exception handling
try {
$conn = new PDO($dsn, $db_user, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected <br><hr>";
}
catch(PDOException $e) {
echo "Connection Failed " . $e->getMessage();
}
try{
// Using Positional Parameter
$sql = "SELECT * FROM student WHERE id = ?";
// Using Named Parameter
// $sql = "SELECT * FROM student WHERE id = :id";
// Prepared Statement
$result = $conn->prepare($sql);
$result->bindValue(1, 4);
// $result->bindValue(':id', 4);
// Execute Prepared statement
$result->execute();
$row = $result->fetch(PDO::FETCH_ASSOC);
echo " ID: " . $row["id"] . " Name: " . $row["name"] . " Roll: " . $row["roll"] . " Address: " . $row["address"] . "<br><br>";
}
catch(PDOException $e) {
echo $e->getMessage();
}
// Close Prepared Statement
unset($result);
// Close Connection
$conn = null;
?>