gpt4 book ai didi

php - PHP问题:mysqli_error()恰好需要1个参数,给定0

转载 作者:行者123 更新时间:2023-11-29 04:01:41 24 4
gpt4 key购买 nike

我只是在一本书中学习一些教程,我试图在我的页面上添加一个“过滤器”选项。因此,当页面加载时,它应该列出所有值,但是如果从下拉列表筛选选项中选择了任何值,则列表应该在页面上更改。这是我的代码:

<?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>&nbsp;</th>
<th>&nbsp;</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)或die(mysqli_error());
提前谢谢

最佳答案

正如您在手册http://php.net/manual/en/mysqli.error.php中看到的,您需要将连接传递到mysqli_error(),因此该行应该是

$conn->query($sql) or die(mysqli_error($conn));

关于php - PHP问题:mysqli_error()恰好需要1个参数,给定0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14587204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com