我只是从书中的一些教程中学习,我正在尝试在页面上放置一个'Filter'选项。因此,在页面加载时,它应该列出所有值,但是如果从下拉列表过滤器选项中选择了任何内容,则列表应该在页面上更改。这是我的代码:
<?php
require_once 'login.php';
$conn = new mysqli ($host, $user, $password, $database) or die("Connection Failed");
$col = 'Blue';
$sql = 'SELECT * FROM users ORDER BY user_creation_date desc';
$result = $conn->query($sql) or die(mysqli_error());
$columns = array('Blue', 'Pink', 'Yellow');
if (isset($_GET['column']) && in_array($_GET['column'], $columns)) {
$col = $_GET['column'];
// prepare the SQL query
$sql = "SELECT * FROM users
WHERE user_pref = $col";
// submit the query and capture the result
$result = $conn->query($sql) or die(mysqli_error());
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Filter by User Pref</title>
</head>
<body>
<form id="form1" method="get" action="">
<label for="column">Order by:</label>
<select name="column" id="column">
<option <?php if ($col == 'Blue') echo 'selected'; ?>>Blue</option>
<option <?php if ($col == 'Pink') echo 'selected'; ?>>Pink</option>
<option <?php if ($col == 'Yellow') echo 'selected'; ?>>Yellow</option>
</select>
<input type="submit" name="change" id="change" value="Change">
</form>
<table>
<tr>
<th scope="col">User name</th>
<th scope="col">User pref</th>
<th> </th>
<th> </th>
</tr>
<?php while($row = $result ->fetch_assoc()) { ?>
<tr>
<td><?php echo $row['user_name']; ?></td>
<td><?php echo $row['user_pref']; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
我得到的错误是
警告:mysqli_error()期望恰好有1个参数,第23行给出0。
这是第23行:
$result = $conn->query($sql) or die(mysqli_error());
正如您在手册http://php.net/manual/en/mysqli.error.php上所看到的,您需要将连接传递给mysqli_error(),因此该行应为
$conn->query($sql) or die(mysqli_error($conn));
您正在混合OOP和函数语法。
mysqli_error();
应该是:
$conn->error;