我正在尝试从PHP中进行查询调用,但我不确定为什么它无法正常工作。看起来我在查询中的操作有问题。在添加where子句和bindParam之前,一切都正常工作。代码执行正确,但在进行查询和绑定后停止。请问是否有人能看到我是否做对了?可能与对gender的post调用有关,我无法输出$gender。感谢任何见解!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Practice Work 5</title>
</head>
<body>
<form action="babynames.php" method = "post">
Year:<br>
<input type="text" name="year">
<input type="submit" value="Submit">
</form>
<select name = "gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</body>
</html>
<?php>
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "baby";
$year = $_POST['year'];
$gender = $_POST['gender'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Year, Name, Ranking, Gender FROM BabyNames where Year = ? and Gender = ?";
$sql -> bindParam (1, $year, PDO::PARAM_INT);
$sql -> bindParam (2, $gender, PDO::PARAM_STR);
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> Year: ". $row["Year"]. " ; Name: ". $row["Name"]. " ; Ranking: " . $row["Ranking"] . " ; Gender: " . $row["Gender"]. " ". "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
=
而不是==
。 - John Conde