-2

可能重复:
mysql_fetch_array() 期望参数 1 是资源,选择中给出的布尔值

警告:mysql_fetch_assoc() 期望参数 1 是资源,在第 36 行的 home/public_html/ 中给出布尔值

警告:mysql_fetch_assoc() 期望参数 1 是资源,在第 42 行的 home/public_html/ 中给出布尔值

在这两行代码中的第 36 行和第 42 行显示错误

请告诉我如何解决这个错误

这是我的代码

<?php

//get date
$button = $_GET['submit'];
$search = $_GET['search'];

if (!$button)
   echo "Type Name";
else
{
    if (strlen($search)<=3)
   echo "The item you searched for was to small";
    else
   {
     echo "You searched for <b>$search</b> <hr size='1'>";

     //connect to database

     mysql_connect('localhost','wh_num','password.');
     mysql_select_db('wh_num');

 //explode search term
           $search_exploded = explode(" ",$search);
           foreach($search_exploded as $search_each)
{
    $str = mysql_real_escape_string(substr($search_each, 0, 6));
    //construct query
    $x++;
    if ($x==1) $construct .= "keyword LIKE '$str%'";
    else       $construct .= " OR keyword LIKE '$str%'";
}

    $construct = "SELECT * FROM location WHERE $construct";
     $run = mysql_query($construct);

     $foundnum = mysql_num_rows($run);

     if ($foundnum==0)
        echo "No results found.";
     {

       echo "$foundnum results found.<p><hr size='1'>";

       while ($runrows = mysql_fetch_assoc($run))
       {

        //get data
        $email = $runrows['email']; 
        $area = $runrows['area'];
          echo "<b><font face=tahoma size=4 color=#600000> Name</b><b> <font face=tahoma size=4 color=#00AAFF> $search<BR></FONT></b>";
        echo "<b><font face=tahoma size=4 color=#600000> Email :</b><b><font face=tahoma size=4 color=#FF9933> $email<BR></FONT></b>
            <b><font face=tahoma size=4 color=#600000> Location :</b><b><font face=tahoma size=4 color=#FF0066> $area<BR></FONT></b>";



       }

     }



    }
}


?>
4

2 回答 2

0

从 else 部分删除 OR 后尝试: else $construct .= "keyword LIKE '$str%'";

于 2012-11-27T06:46:31.663 回答
0

当资源为假时,您的查询失败....我认为您的错误是您所在位置的连接...

使用数组来创建正确的 mysql where 语句:

$where = array();
if ($x==1) $where[] = "keyword LIKE '$str%'";
else       $where[] = "keyword LIKE '$str%'";


$construct = "SELECT * FROM location";
if(count($where) > 0) {
    $construct .= ' WHERE '.implode(' OR ', $where);
}

你的代码

<?php

//get date
$button = $_GET['submit'];
$search = $_GET['search'];

if (!$button)
    echo "Type Name";
else
{
    if (strlen($search)<=3)
        echo "The item you searched for was to small";
    else
    {
        echo "You searched for <b>$search</b> <hr size='1'>";

        //connect to database

        $link = mysql_connect('localhost','wh_num','password.');
        mysql_select_db('wh_num');

        //explode search term
        $search_exploded = explode(" ",$search);
        $where = array();
        foreach($search_exploded as $search_each)
        {
            $str = mysql_real_escape_string(substr($search_each, 0, 6));
            //construct query

            if ($x==1) $where[] = "keyword LIKE '$str%'";
            else       $where[] = "keyword LIKE '$str%'";
        }

        $construct = "SELECT * FROM location";
        if(count($where) > 0) {
            $construct .= ' WHERE '.implode(' OR ', $where);
        }
        $run = mysql_query($construct);
                if(strlen(mysql_error($link)) > 0) {
                         echo 'SQL error: '.mysql_error($link); exit;
                    }
        $foundnum = mysql_num_rows($run);

        if ($foundnum==0)
            echo "No results found.";
        {

            echo "$foundnum results found.<p><hr size='1'>";

            while ($runrows = mysql_fetch_assoc($run))
            {

                //get data
                $email = $runrows['email'];
                $area = $runrows['area'];
                echo "<b><font face=tahoma size=4 color=#600000> Name</b><b> <font face=tahoma size=4 color=#00AAFF> $search<BR></FONT></b>";
                echo "<b><font face=tahoma size=4 color=#600000> Email :</b><b><font face=tahoma size=4 color=#FF9933> $email<BR></FONT></b>
            <b><font face=tahoma size=4 color=#600000> Location :</b><b><font face=tahoma size=4 color=#FF0066> $area<BR></FONT></b>";



            }

        }



    }
}


?>
于 2012-11-27T06:17:29.397 回答