1// Multiple rows:
2$sql = "SELECT * FROM users WHERE id=?";
3$stmt = $conn->prepare($sql);
4$stmt->bind_param("i", $id);
5$stmt->execute();
6$result = $stmt->get_result();
7while ($row = $result->fetch_assoc()) {
8 echo $row['name'];
9}
10
11//Single row:
12$sql = "SELECT user FROM users WHERE id=?"; // SQL with parameters
13$stmt = $conn->prepare($sql);
14$stmt->bind_param("i", $id);
15$stmt->execute();
16$result = $stmt->get_result(); // get the mysqli result
17$user = $result->fetch_assoc(); // fetch data
18
19// Update/delete from
20$sql = "UPDATE users SET rank=?, status=? WHERE id=?"; // SQL with parameters
21$sql = "DELETE FROM users WHERE id=?";
22$stmt = $conn->prepare($sql);
23
24$stmt->bind_param('sss', $rank, $status, $id);
25$stmt->execute();
1$servername = "localhost";
2$username = "username";
3$password = "password";
4$dbname = "myDB";
5
6$conn = new mysqli($servername, $username, $password, $dbname);
7
8if ($conn->connect_error) {
9 die("Connection failed: " . $conn->connect_error);
10}
11
12$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
13$stmt->bind_param("sss", $firstname, $lastname, $email);
14
15$firstname = "John";
16$lastname = "Doe";
17$email = "john@example.com";
18$stmt->execute();
19
20echo "New records created successfully";
21
22$stmt->close();
23$conn->close();
1$stmt = $mysqli->prepare("SELECT * FROM myTable WHERE name = ? AND age = ?");
2$stmt->bind_param("si", $_POST['name'], $_POST['age']);
3$stmt->execute();
4$stmt->close();
1$stmt->bind_param("i", $data); // Type: Integer
2$stmt->bind_param("d", $data); // Type: Double
3$stmt->bind_param("s", $data); // Type: String
4$stmt->bind_param("b", $data); // Type: Blob
1$array_of_values = array( "Brasil", "Argentina" );
2$types = "ss";
3$mysqli_stmt->bind_param( $types, ...$array_of_values );
4