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 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?