不幸的是,使用“/* 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();
?>