-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path89. PDOPrepInsert1.php
46 lines (38 loc) · 1.08 KB
/
89. PDOPrepInsert1.php
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
<?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 Anonymous Positional Placeholder
$sql = "INSERT INTO student (name, roll, address) VALUES (?, ?, ?)";
// Prepared Statement
$result = $conn->prepare($sql);
// Execute Prepared Statement
$result->execute(array('Rohan', 106, 'Kolkata'));
/*
// Variables and Values
$name = "Ragini";
$roll = 107;
$address = "Kolkata";
$result->execute(array($name, $roll, $address));
*/
echo $result->rowCount() . " Row Inserted <br>";
}
catch(PDOException $e) {
echo $e->getMessage();
}
// Close Prepared Statement
unset($result);
// Close Connection
$conn = null;
?>