-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path75. MySQLiOOPPrepInsert.php
More file actions
59 lines (43 loc) · 1.28 KB
/
75. MySQLiOOPPrepInsert.php
File metadata and controls
59 lines (43 loc) · 1.28 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
50
51
52
53
54
55
56
57
58
59
<?php
$db_host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "test_db";
// Create Connection
$conn = new mysqli($db_host, $db_user, $db_password, $db_name);
// Check Connection
if($conn->connect_error){
die("Connection Failed");
}
echo "Connected Successfully <hr>";
// INSERT SQL statement
$sql = "INSERT INTO student (name, roll, address) VALUES (?, ?, ?)";
// Prepared Statement
$result = $conn->prepare($sql);
if($result) {
// Bind Variables to Prepared Statement as Parameters
$result->bind_param('sis', $name, $roll, $address);
// Variables and values
$name = "Soni";
$roll = 108;
$address = "Kolkata";
// Execute Prepared statement
$result->execute();
// We can write another variable and value set then execute statement.
// It will insert another row with new value for example: -
/*
// Another Variables and values set
$name = "Veeru";
$roll = 109;
$address = "Ranchi";
// Execute Prepared Statement
$result->execute();
*/
// Number of row affected
echo $result->affected_rows . " Row Inserted <br>" ;
}
// close prepared statement
$result->close();
// Close Connection
$conn->close();
?>