不幸的是,使用 "/* bind result variables */ $stmt->bind_result($district);" 已过时且不建议使用。
<?php
$mysqli = new mysqli("localhost", "test", "test", "test");
if ($mysqli->character_set_name()!="utf8mb4") { $mysqli->set_charset("utf8mb4"); }
$secondname = "Ma%";
$types = "s";
$parameters = array($secondname);
$myquery = "select * from users where secondname like ?";
if ($stmt = $mysqli->prepare($myquery)) {
$stmt->bind_param($types, ...$parameters);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
$numrows = $result->num_rows;
while($row = $result->fetch_assoc()) {
echo $row['firstname']." ".$row['secondname']."<br />";
}
}
$mysqli->close();
?>
此外,不要使用 `$stmt->bind_param("s", $city);`,而是使用 `$stmt->bind_param($types, ...$parameters);` 和数组。 这里使用数组 (`$parameters`) 的优势已经很明显了, 可以使用一个包含 5 个元素的数组,而不是 5 个变量。
<?php
$mysqli = new mysqli("localhost", "test", "test", "test");
if ($mysqli->character_set_name()!="utf8mb4") { $mysqli->set_charset("utf8mb4"); }
$uid = intval($_POST['uid']);
$length=15; $account = mb_substr(trim($_POST['account']),0,$length,"utf-8"); $account=strip_tags($account);
$length=50; $password = mb_substr(trim($_POST['password']),0,$length,"utf-8"); $password = password_hash($password, PASSWORD_DEFAULT);
$length=25; $prijmeni = mb_substr(trim($_POST['prijmeni']),0,$length,"utf-8"); $prijmeni=strip_tags($prijmeni);
$length=25; $firstname = mb_substr(trim($_POST['firstname']),0,$length,"utf-8"); $firstname=strip_tags($firstname); $firstname = str_replace(array(">","<",'"'), array("","",""), $firstname);
$dotaz = "UPDATE users SET account = ?, password = ?, secname = ?, firstname = ? WHERE uid = ?";
$types = "ssssi";
$parameters = array($account,$password,$prijmeni,$firstname,$uid);
if ($stmt = $mysqli->prepare($dotaz)) {
$stmt->bind_param($types, ...$parameters);
$stmt->execute();
echo $stmt->affected_rows;
$stmt->close();
}
$mysqli->close();
?>