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//on sql server
2public function get_data_from($id){
3 $request = "SELECT * FROM tableName WHERE id = ?";
4
5 //preparing the request
6 $stmt = $this->dbh->prepare($request);
7
8 //executing the request
9 $stmt->execute( array($id) );
10
11 //fetching the result of the request
12 $result = $stmt->fetchAll();
13
14 return $result;
15}
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 = $mysqli->prepare("UPDATE myTable SET name = ? WHERE id = ?");
2$stmt->bind_param("si", $_POST['name'], $_SESSION['id']);
3$stmt->execute();
4$stmt->close();
5Copy
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