-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest
80 lines (64 loc) · 2 KB
/
test
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Prepare a statement
$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username); // "s" denotes the type (string) of the parameter
$username = $_POST['username'];
// Execute the query
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo "User: " . $row['username'];
}
// Close the statement and connection
$stmt->close();
$mysqli->close();
?>
<?php
try {
$pdo = new PDO("mysql:host=localhost;dbname=database", "user", "password");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Prepare a statement
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $username);
$username = $_POST['username'];
// Execute the query
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "User: " . $row['username'];
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
// Close the connection
$pdo = null;
?>
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Prepare a statement with multiple parameters
$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ? AND email = ? AND age = ?");
$stmt->bind_param("ssi", $username, $email, $age); // "s" denotes string, "i" denotes integer
// Assigning values to the variables
$username = $_POST['username'];
$email = $_POST['email'];
$age = (int)$_POST['age'];
// Execute the query
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo "User: " . $row['username'] . ", Email: " . $row['email'] . ", Age: " . $row['age'];
}
// Close the statement and connection
$stmt->close();
$mysqli->close();
?>
$stmt->close();
$mysqli->close();