【发布时间】:2015-03-31 14:12:45
【问题描述】:
我在运行下面的脚本时出现错误,我不知道如何更正它。本质上,我在mysql中有一个名为gamestbl的表。我希望用户输入一些关于游戏名称或游戏描述的关键词,然后 php 将正确的游戏显示为下拉列表(理想情况下),甚至显示为信息表。
错误提示:
警告:mysqli_query() 至少需要 2 个参数,1 个在 C:\xampp\htdocs\introducingphp\Email.php 第 29 行给出
与 警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,在第 56 行的 C:\xampp\htdocs\introducingphp\Email.php 中给出 null
第 56 行是我的 While 语句。 第 29 行是 ....$q=mysqli_query($sql);
<!DOCTYPE HTML>
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
//Connect to the Database
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "DON'T TELL*";
$dbname = "Games";
//Create connection
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
// Show error if connection fails
if (!$conn){
die("Connection failed: " .
mysqli_connect_error());
}
if(isset($_REQUEST['submit'])){
$search=$_POST['search'];
$sql=" SELECT * FROM gamestbl WHERE game_name LIKE '%".$search."%' OR game_description LIKE '%".$search."%'";
$q=mysqli_query($sql);
}
else{
$sql="SELECT * FROM gamestbl";
$q=mysqli_query($sql);
}
?>
<title>Experiment</title>
</head>
<body>
<form method="post">
<table width="200" border="1">
<tr>
<td>Search</td>
<td><input type="text" name="search" value="" /></td>
<td><input type="submit" name="submit" value=" Find " /></td>
</tr>
</table>
</form>
<table>
<tr>
<td>Game ID</td>
<td>Game Name</td>
<td>Game Description</td>
<td>Game Genre</td>
<td>Requested Days</td>
<td>Game Price</td>
</tr>
<?php
while($res=mysqli_fetch_array($q)){
?>
<tr>
<td><?php echo $res['game_id'];?></td>
<td><?php echo $res['game_name'];?></td>
<td><?php echo $res['game_description'];?></td>
<td><?php echo $res['game_genre'];?></td>
<td><?php echo $res['requested_days'];?></td>
<td><?php echo $res['game_price'];?></td>
</tr>
<?php }?>
</table>
</body>
</html>
对不起,如果这听起来很糟糕 - 我是 mysqli 和 php 的真正初学者
【问题讨论】:
-
在使用while循环之前先调试
print '<pre>'; print_r($q); -
mysql_query()如果发生某些 SQL 错误,则返回FALSE。检查您的$q,如果是FALSE,请使用mysql_error()查看该错误。 -
可能在你的 $search 中引用了一些引号,以此作为学习准备好的语句的借口。而且你正在混合 mysqli 和 mysql
-
你正在混合 mysql 和 mysqli ..正如我上面的人所说的那样。将 mysql_fatch_array() 函数更改为 mysqli_fatch_array()
-
@CodingHorror 嗯,我是否应该使用 mysqli 更改所有 mysql 实例以保持一致?我还有两个 mysql 语句(请参阅 IF(isset .....。没有意识到这两者不能互换使用。