警告:mysqli_fetch_array()期望参数1为mysqli_result,给定布尔值

时间:2014-04-14 10:23:27

标签: php mysql

WTF? - 这正在变成一场噩梦,似乎在错误后得到错误。我愿意为某人付出时间只是为了帮助我实现看似无法实现的目标,请...我想要的是一个下拉菜单,从中选择数据根据其类别的表格并显示它..帮助! :(

<form action="portfolio.php" method="post">
 <select onload="displayProject(this.value);" onchange="displayProject(this.value);">
  <option value='none'>All</option>
  <option value='1'>Fencing</option>
  <option value='2'>Driveway</option>
 </select>
</form>

<?php 

$db = new mysqli('localhost', 'wlarter_user', 'pw', 'wlarter_portfolio');

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

$option= $_POST['option'];

$queries = "SELECT * FROM image"; 
if ($option != 'none'){
 $queries = "SELECT * FROM image where category=".$option; 
}

$result=@mysqli_query($db,"$queries");

while($row = mysqli_fetch_array($result))
{
?>

<div class="box-portfolio"> <?php echo $row['Img']; ?> </div>


<?php
}                      
mysqli_close($db);
?>

4 个答案:

答案 0 :(得分:1)

简单地转过身来,可能就是你想要的:

$queries=$query;

$query = $queries;

完整代码:

<FORM action="portfolio.php" method="post">
    <SELECT onload="displayProject(this.value);" onchange="displayProject(this.value);">
        <OPTION VALUE='none'>All</OPTION>
        <OPTION VALUE='1'>Fencing</OPTION>
        <OPTION VALUE='2'>Driveway</OPTION>
    </SELECT>
</FORM>

<?php

$db = new mysqli('localhost', 'wlarter_user', 'pw', 'wlarter_portfolio');

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

$option= $_POST['option'];

$queries = "SELECT * FROM image";
if ($option != 'none'){
    $queries = "SELECT * FROM image where category=".$option;
}

$query=$queries;
$result=mysqli_query($db,"$query");

while($row = mysqli_fetch_array($result))
{
    ?>

    <div class="box-portfolio"> <?php echo $row['Img']; ?> </div>


<?php
}
mysqli_close($db);
?>

答案 1 :(得分:0)

你可以尝试这样的.......

$result=@mysqli_query($db,"$queries");

答案 2 :(得分:0)

替换此行

$result=@mysqli_query($db,"$queries");

用这个:

$result=@mysqli_query($db,$queries);

它可能有所帮助..

答案 3 :(得分:0)

因为mysql查询的结果可以是资源或布尔值(如果失败),您应该在执行后查看查询结果:

if ($result !== false) {
    while($row = mysqli_fetch_array($result))
    {
        ?>
            <div class="box-portfolio"> <?php echo $row['Img']; ?> </div>
        <?php
    }
}
else {
    // do your error message stuff here
    echo mysqli_error($db);
}