-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path90. PDOPrepInsert1.php
More file actions
49 lines (39 loc) · 1.22 KB
/
90. PDOPrepInsert1.php
File metadata and controls
49 lines (39 loc) · 1.22 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 Named Placeholder
$sql = "INSERT INTO student (name, roll, address) VALUES (:name, :roll, :address)";
// Prepared Statement
$result = $conn->prepare($sql);
// Bind Parameter to Prepared Statement
$result->bindParam(':name', $name, PDO::PARAM_STR);
$result->bindParam(':roll', $roll, PDO::PARAM_INT);
$result->bindParam(':address', $address, PDO::PARAM_STR);
// Variables and Values
$name = "Ragini";
$roll = 107;
$address = "Kolkata";
// Execute Prepared Statement
$result->execute();
echo $result->rowCount() . " Row Inserted <br>";
}
catch(PDOException $e) {
echo $e->getMessage();
}
// Close Prepared Statement
unset($result);
// Close Connection
$conn = null;
?>