PHP问题:mysqli_error()期望恰好有1个参数,给定0 [英] PHP issue: mysqli_error() expects exactly 1 parameter, 0 given

查看:53
本文介绍了PHP问题:mysqli_error()期望恰好有1个参数,给定0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是从书中的一些教程中学习,并且试图在页面上放置过滤器"选项.因此,在页面加载时,它应该列出所有值,但是如果从下拉列表过滤器选项中选择了任何内容,则列表应在页面上更改.这是我的代码:

I'm just working from some tutorials in a book, and I'm trying to put a 'Filter' option on my page. So when the page loads it should list all the values but if anything is selected from the dropdown list filter option then the list should change on the page. Here's my code:

<?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.

Warning: mysqli_error() expects exactly 1 parameter, 0 given on line 23.

这是第23行:

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

推荐答案

正如您在手册

As you can see on the manual http://php.net/manual/en/mysqli.error.php , you need to pass the connection to mysqli_error(), so that line should be

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

这篇关于PHP问题:mysqli_error()期望恰好有1个参数,给定0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆