PHP问题:mysqli_error()只需要1个参数,给出0

时间:2013-01-29 16:12:22

标签: php mysql

我只是在书中的一些教程中工作,我正在尝试在我的页面上添加“过滤器”选项。因此,当页面加载时,它应列出所有值,但如果从下拉列表过滤器选项中选择任何内容,则列表应在页面上更改。这是我的代码:

<?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()期望在第23行给出1个参数0,这是第23行:   $ result = $ conn-&gt; query($ sql)或die(mysqli_error());

提前致谢

2 个答案:

答案 0 :(得分:5)

你正在混合OOP和功能语法。

mysqli_error();

应该是:

$conn->error;

答案 1 :(得分:2)

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

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