dongwuchu0314 2013-01-29 16:12
浏览 313
已采纳

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>

The error I'm getting is

Warning: mysqli_error() expects exactly 1 parameter, 0 given on line 23. This is line 23: $result = $conn->query($sql) or die(mysqli_error());

Thanks in advance

  • 写回答

2条回答 默认 最新

  • douyi6818 2013-01-29 16:14
    关注

    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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加