-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path65. MySQLiProPrepInsert.php
More file actions
55 lines (43 loc) · 1.35 KB
/
65. MySQLiProPrepInsert.php
File metadata and controls
55 lines (43 loc) · 1.35 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
<?php
$db_host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "test_db";
// Create connection
$conn = mysqli_connect($db_host, $db_user, $db_password, $db_name);
// Check connection
if (!$conn) {
// die("Connection failed");
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully <hr>";
// INSERT SQL statement
$sql = "INSERT INTO student (name, roll, address) VALUES (?, ?, ?)";
// Prepared Statement
$result = mysqli_prepare($conn, $sql);
if($result) {
// Bind Variables to Prepared Statement as Parameters
mysqli_stmt_bind_param($result, 'sis', $name, $roll, $address);
// Variables and values
$name = "Soni";
$roll = 108;
$address = "Kolkata";
// Execute Prepared Statement
mysqli_stmt_execute($result);
// 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
mysqli_stmt_execute($result);
*/
echo mysqli_stmt_affected_rows($result) . "Row Inserted <br>" ;
}
// close prepared statement
mysqli_stmt_close($result);
// Close Connection
mysqli_close($conn);
?>