错误mysql_num_rows()期望参数1是资源,给定布尔值

时间:2012-11-27 06:13:49

标签: php mysql

  

可能重复:
  mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

警告: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>";



       }

     }



    }
}


?>

2 个答案:

答案 0 :(得分:0)

当资源为false时,您的查询失败.... 我认为你的错误就是你所在的地方......

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

$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>";



            }

        }



    }
}


?>

答案 1 :(得分:0)

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