1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Search Form PHP BOOLEAN ERROR NEED HELP

Discussion in 'PHP' started by terriblebmx, Apr 28, 2013.

  1. #1
    Hey guys new to the forum and also my reason for registering! so hi lol

    I have a search form on index.php that keeps giving me this error

    form follows as is...

    
    <form action="search.php" method="post">
    <input type="text" value="Search on this website" name="q" size="10" id="searchfield" title="searchfield" onfocus="clearText(this)" onblur="clearText(this)" />
    <input type="submit" name="Search" value="search" id="searchbutton" title="Search" /></form>
    
    PHP:
    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/blameyo/public_html/search.php on line 31

    the search.php page contains the script

    
    <?php $input = $_GET['input'];
          $terms = explode(" ", $input);
          $query = "SELECT * FROM search WHERE ";
    
          foreach ($terms as $each) {
          $i++;
          if ($i == 1)
          $query  .= "keywords LIKE '%each%' ";
          else
          $query  .= "keywords LIKE  '%each%'  "; }
    
    //Connect to Database
    
    mysql_connect ('xxxxx', 'xxxxxxx', 'xxxxxxx');
    mysql_select_db ("xxxxxxxx");
    $query = mysql_query($query);
    $numrows = mysql_num_rows($query);
    if ($numrows > 0) {
    
    while ($row = mysqlfetch_assoc($query)){
    $id = $row['id'];
    $title = $row['title'];
    $description = $row['description'];
    $keywords = $row['keywords'];
    $link = $row['link'];
    
    echo "<h2><a href='$link'>$title</a></h2>
    $description<br/><br/>"; } }
    
    else
    echo "No results found for \"<b>$input</b>\"";
    
    //Disconnect 
    
    mysql_close();
    
    ?>
    
    PHP:
    you think it maybe a script problem or how i set up my database in myadmin ?
     
    Solved! View solution.
    terriblebmx, Apr 28, 2013 IP
  2. temp2

    temp2 Well-Known Member

    Messages:
    1,228
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    150
    Digital Goods:
    2
    #2
    your $query is not valid, missed AND

    but I think this search.php contains more lines?
     
    temp2, Apr 28, 2013 IP
  3. wren11

    wren11 Active Member

    Messages:
    89
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    53
    #3
    without seeing your full query i can only assume that your missing and AND also, Shouldn't it be '%$each%'
    Also your mixing your variables strangely!, it's bad practice to use variables for one thing and assign them to another thing, try
    $query_results = mysql_query($query);
    PHP:
     
    wren11, Apr 28, 2013 IP
  4. terriblebmx

    terriblebmx Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    hey thanks for the reply guys, and I'm new to php and decided to write the script as is in a txt pad following a tutorial on youtube.

    i get this error after writing in what wren11 asked me to try

    Warning: mysql_num_rows() expects parameter 1 to be resource, string given in /home/blameyo/public_html/search.php on line 32

    full query is

    
    <html xmlns="">
     
    <body>
     
    <h1 style="color;#09F; font-size;36px;">SEARCH</h1>
    <form action"search.php" method="get">
    <input type="text" name="input" size"50" <?php echo $_GET ['input']; ?> />
    <input type="submit" value="search" />
    </form>
     
    <hr/>
     
     
    <?php
    $input = $_GET['input'];//Note to self $input in the name of the search feild
    $terms = explode(" ", $input);
    $query = "SELECT * FROM search WHERE ";
     
    foreach ($terms as $each){
    $i++;
    if ($i == 1)
    $query .= "keywords LIKE '%$each%' ";
    else
    $query .= "OR keywords LIKE '%$each%' ";
    }
     
    // connecting to our mysql database
    mysql_connect("localhost", "blameyo_xxx", "xxx");
    mysql_select_db("blameyo_xxx");
     
    $query_results = mysql_query($query);
    $numrows = mysql_num_rows($query);
    if ($numrows > 0){
     
    while ($row = mysql_fetch_assoc($query)){
    $id = $row['id'];
    $title = $row['title'];
    $description = $row['description'];
    $keywords = $row['keywords'];
    $link = $row['link'];
    echo "<h2><a href='$link'>$title</a></h2>
    $description<br /><br />";
     
    }
     
    }
    else
    echo "No results found for \"<b>$input</b>\"";
     
    // disconnect
    mysql_close();
    ?>
    </body>
    </html>
    
    PHP:
     
    terriblebmx, Apr 28, 2013 IP
  5. wren11

    wren11 Active Member

    Messages:
    89
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    53
    #5
    Try this:

    
        $query_results = mysql_query($query);
        $numrows = mysql_num_rows($query_results, $conn); // $conn should be the link to the connection
     
        if ($numrows > 0)
        {     
            while ($row = mysql_fetch_assoc($query_results))
            {
                $id = $row['id'];
                $title = $row['title'];
                $description = $row['description'];
                $keywords = $row['keywords'];
                $link = $row['link'];
         
                echo "<h2><a href='$link'>$title</a></h2>$description<br /><br />";
         
            }
        }
        else
            echo "No results found for \"<b>$input</b>\"";
    PHP:
     
    Last edited: Apr 28, 2013
    wren11, Apr 28, 2013 IP
  6. wren11

    wren11 Active Member

    Messages:
    89
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    53
    #6
    If not, I think this should work?

      
     
       $conn = mysql_connect("localhost", "xxxx", "xxxx");
        mysql_select_db("blameyo_xxx");
     
        $query_results = mysql_query($query, $conn);
       
        if (isset($query_results))
        {
            while ($row = mysql_fetch_array($query_results))
            {
                $id = $row['id'];
                $title = $row['title'];
                $description = $row['description'];
                $keywords = $row['keywords'];
                $link = $row['link'];
           
                echo "<h2><a href='$link'>$title</a></h2>$description<br /><br />";       
            }   
        }
        else
            echo "No results found for \"<b>$input</b>\"";
    PHP:
     
    wren11, Apr 28, 2013 IP
  7. wren11

    wren11 Active Member

    Messages:
    89
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    53
    #7
    Check http://php.net/manual/en/function.mysql-num-rows.php for some examples!
     
    wren11, Apr 28, 2013 IP
  8. terriblebmx

    terriblebmx Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/blameyo/public_html/search.php on line 37

    thats my error code now.

    i have index.php and search.php both in public_html so that should be right ?
    ummm i created the database as it is in the script and table as well which is search

    dunno :|

    ill check that out though
     
    terriblebmx, Apr 28, 2013 IP
  9. wren11

    wren11 Active Member

    Messages:
    89
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    53
    #9
    are you sure the query is yielding any results? try and test the query
     
    wren11, Apr 28, 2013 IP
  10. terriblebmx

    terriblebmx Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #10
    echo "$num_rows Rows\n";

    does " Rows\n"; " have to be changed to anything ?
     
    terriblebmx, Apr 28, 2013 IP
  11. terriblebmx

    terriblebmx Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #11
    im not yeilding any results. my last test gave me this as an error


    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/blameyo/public_html/search.php on line 18

    script now as in the php manual

    
    <?php
            $input = $_GET['input'];//Note to self $input in the name of the search feild
            $terms = explode(" ", $input);
            $query = "SELECT * FROM search WHERE ";
           
            foreach ($terms as $each){
                $i++;
                if ($i == 1)
                    $query .= "keywords LIKE '%$each%' ";
                else
                    $query .= "OR keywords LIKE '%$each%' ";
            }
     
    $link = mysql_connect("localhost", "blameyo_xxx", "xxx");
    mysql_select_db("blameyo_xxx", $link);
     
    $result = mysql_query("SELECT * FROM search", $link);
    $num_rows = mysql_num_rows($result);
     
    echo "$num_rows Rows\n";
    if ($numrows > 0){
     
                while ($row = mysql_fetch_assoc($query)){
                    $id = $row['id'];
                    $title = $row['title'];
                    $description = $row['description'];
                    $keywords = $row['keywords'];
                    $link = $row['link'];
                    echo "<h2><a href='$link'>$title</a></h2>
                    $description<br /><br />";
     
                }
               
            }
            else
                echo "No results found for \"<b>$input</b>\"";
           
            // disconnect
            mysql_close();
     
     
    ?>
    
    PHP:
     
    terriblebmx, Apr 28, 2013 IP
  12. wren11

    wren11 Active Member

    Messages:
    89
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    53
    #12
    <?php
     
    $input = $_GET['input'];
    $terms = explode(" ", $input);
    $query = "SELECT * FROM `search` WHERE ";
     
     
    $i = 0;
     
    foreach ($terms as $each)
    {
        if (($i++ % 2) == 0)
            $query .= "`keywords` LIKE '%$each%' ";
        else
            $query .= "OR `keywords` LIKE '%$each%' ";
    }
     
    $link = mysql_connect("127.0.0.1", "xxx", "xxx");
    mysql_select_db("blameyo_xxx", $link);
     
    $results = mysql_query($query, $link);
     
    if (mysql_num_rows($results) > 0)
    {
        while ($row = mysql_fetch_Array($results))
        {
            echo $row['ID'] . "<BR />";
        }
    }
    else
    {
        echo "No Results found.";   
    }
     
     
    mysql_close($link);
     
    ?>
    PHP:
    I tested this on my computer and it worked
     
    wren11, Apr 28, 2013 IP
  13. terriblebmx

    terriblebmx Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #13
    im gonna record a short video of what im doing and hopefully my errors will be shown. be back in a short while !
     
    terriblebmx, Apr 28, 2013 IP
  14. terriblebmx

    terriblebmx Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #14


    please watch guys. It's 15 mins long but detailed from the start and hopefully will show some error of mine in the process. it starts off with me making the database! thanks. commentary included
     
    terriblebmx, Apr 29, 2013 IP
  15. wren11

    wren11 Active Member

    Messages:
    89
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    53
    #15
    This should be working, Change the db stuff to yours

    
    <html xmlns="">
     
    <body>
     
    <h1 style="color;#09F; font-size;36px;">SEARCH</h1>
    <form action="search.php" method="get">
    <input type=text name=input size=50>
    <input type="submit" value="search" />
    </form>
     
    <hr/>
     
     
    <?php
     
    $input = $_GET['input'];
     
    if (isset($input))
    {
        $terms = explode(" ", $input);
        $query = "SELECT * FROM `search` WHERE ";
     
     
        $i = 0;
     
        foreach ($terms as $each)
        {
        if (($i++ % 2) == 0)
            $query .= "`keywords` LIKE '%$each%' ";
        else
            $query .= "OR `keywords` LIKE '%$each%' ";
        }
     
        $link = mysql_connect("localhost", "tester", "xxxx");
        mysql_select_db("xxx", $link);
     
        $results = mysql_query($query, $link);
     
        if (mysql_num_rows($results) > 0)
        {
            while ($row = mysql_fetch_Array($results))
            {
                $id = $row['id'];
                $title = $row['title'];
                $description = $row['description'];
                $keywords = $row['keywords'];
                $link = $row['link'];
                echo "<h2><a href='$link'>$title</a></h2>$description<br /><br />";
            }
        }
        else
        {
            echo "No Results found.";
        }
     
     
        mysql_close();
    }
     
    ?>
     
     
    </body>
    </html>
    PHP:
    Working Example: http://www.citfree.com/dp/search.php (search for test)
     
    wren11, Apr 29, 2013 IP
  16. terriblebmx

    terriblebmx Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #16
    hey wren11 i think this is a better script. i dont get the error when i click search on the index.php but yeild no results and when i do click search on the search.php i get an error and nor do i yeild results


    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/blameyo/public_html/search.php on line 39

    in my videos theres "insert" where i have to enter a description, ID a link and whatever else. could that be where the problem lies ? it may not be the script
     
    terriblebmx, Apr 29, 2013 IP
  17. #17
    okay, you must have a problem on the database end my friend, as That script is working for me on my database, If you want you can PM me your DB test info and i can test it for you.
     
    wren11, Apr 29, 2013 IP
  18. terriblebmx

    terriblebmx Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #18
    hey thanks alot for your help and patience wren11. I figured out what I was doing wrong that night...

    the user has to have all privileges enabled. I never gave the user privileges because i didn't understand what it was doing like giving the visitor permission to do this and that...

    but that corrected my boolean error.

    i gave you best answer since you cant go wrong with someone personally helping you so thanks again!
     
    terriblebmx, May 2, 2013 IP