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...

    PHP:
    1.  
    2. <form action="search.php" method="post">
    3. <input type="text" value="Search on this website" name="q" size="10" id="searchfield" title="searchfield" onfocus="clearText(this)" onblur="clearText(this)" />
    4. <input type="submit" name="Search" value="search" id="searchbutton" title="Search" /></form>
    5.  
    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:
    1.  
    2. <?php $input = $_GET['input'];
    3.       $terms = explode(" ", $input);
    4.       $query = "SELECT * FROM search WHERE ";
    5.  
    6.       foreach ($terms as $each) {
    7.       $i++;
    8.       if ($i == 1)
    9.       $query  .= "keywords LIKE '%each%' ";
    10.       else
    11.       $query  .= "keywords LIKE  '%each%'  "; }
    12.  
    13. //Connect to Database
    14.  
    15. mysql_connect ('xxxxx', 'xxxxxxx', 'xxxxxxx');
    16. mysql_select_db ("xxxxxxxx");
    17. $query = mysql_query($query);
    18. $numrows = mysql_num_rows($query);
    19. if ($numrows > 0) {
    20.  
    21. while ($row = mysqlfetch_assoc($query)){
    22. $id = $row['id'];
    23. $title = $row['title'];
    24. $description = $row['description'];
    25. $keywords = $row['keywords'];
    26. $link = $row['link'];
    27.  
    28. echo "<h2><a href='$link'>$title</a></h2>
    29. $description<br/><br/>"; } }
    30.  
    31. else
    32. echo "No results found for \"<b>$input</b>\"";
    33.  
    34. //Disconnect
    35.  
    36.  
    37. ?>
    38.  
    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,206
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    100
    Digital Goods:
    1
    #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 Member

    Messages:
    80
    Likes Received:
    5
    Best Answers:
    3
    Trophy Points:
    43
    #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
    PHP:
    1. $query_results = mysql_query($query);
    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

    PHP:
    1.  
    2. <html xmlns="">
    3.  
    4. <body>
    5.  
    6. <h1 style="color;#09F; font-size;36px;">SEARCH</h1>
    7. <form action"search.php" method="get">
    8. <input type="text" name="input" size"50" <?php echo $_GET ['input']; ?> />
    9. <input type="submit" value="search" />
    10. </form>
    11.  
    12. <hr/>
    13.  
    14.  
    15. <?php
    16. $input = $_GET['input'];//Note to self $input in the name of the search feild
    17. $terms = explode(" ", $input);
    18. $query = "SELECT * FROM search WHERE ";
    19.  
    20. foreach ($terms as $each){
    21. $i++;
    22. if ($i == 1)
    23. $query .= "keywords LIKE '%$each%' ";
    24. else
    25. $query .= "OR keywords LIKE '%$each%' ";
    26. }
    27.  
    28. // connecting to our mysql database
    29. mysql_connect("localhost", "blameyo_xxx", "xxx");
    30. mysql_select_db("blameyo_xxx");
    31.  
    32. $query_results = mysql_query($query);
    33. $numrows = mysql_num_rows($query);
    34. if ($numrows > 0){
    35.  
    36. while ($row = mysql_fetch_assoc($query)){
    37. $id = $row['id'];
    38. $title = $row['title'];
    39. $description = $row['description'];
    40. $keywords = $row['keywords'];
    41. $link = $row['link'];
    42. echo "<h2><a href='$link'>$title</a></h2>
    43. $description<br /><br />";
    44.  
    45. }
    46.  
    47. }
    48. else
    49. echo "No results found for \"<b>$input</b>\"";
    50.  
    51. // disconnect
    52. ?>
    53. </body>
    54. </html>
    55.  
    terriblebmx, Apr 28, 2013 IP
  5. wren11

    wren11 Member

    Messages:
    80
    Likes Received:
    5
    Best Answers:
    3
    Trophy Points:
    43
    #5
    Try this:

    PHP:
    1.  
    2.     $query_results = mysql_query($query);
    3.     $numrows = mysql_num_rows($query_results, $conn); // $conn should be the link to the connection
    4.  
    5.     if ($numrows > 0)
    6.     {    
    7.         while ($row = mysql_fetch_assoc($query_results))
    8.         {
    9.             $id = $row['id'];
    10.             $title = $row['title'];
    11.             $description = $row['description'];
    12.             $keywords = $row['keywords'];
    13.             $link = $row['link'];
    14.      
    15.             echo "<h2><a href='$link'>$title</a></h2>$description<br /><br />";
    16.      
    17.         }
    18.     }
    19.     else
    20.         echo "No results found for \"<b>$input</b>\"";
    Last edited: Apr 28, 2013
    wren11, Apr 28, 2013 IP
  6. wren11

    wren11 Member

    Messages:
    80
    Likes Received:
    5
    Best Answers:
    3
    Trophy Points:
    43
    #6
    If not, I think this should work?

    PHP:
    1.  
    2.  
    3.    $conn = mysql_connect("localhost", "xxxx", "xxxx");
    4.     mysql_select_db("blameyo_xxx");
    5.  
    6.     $query_results = mysql_query($query, $conn);
    7.    
    8.     if (isset($query_results))
    9.     {
    10.         while ($row = mysql_fetch_array($query_results))
    11.         {
    12.             $id = $row['id'];
    13.             $title = $row['title'];
    14.             $description = $row['description'];
    15.             $keywords = $row['keywords'];
    16.             $link = $row['link'];
    17.        
    18.             echo "<h2><a href='$link'>$title</a></h2>$description<br /><br />";      
    19.         }  
    20.     }
    21.     else
    22.         echo "No results found for \"<b>$input</b>\"";
    wren11, Apr 28, 2013 IP
  7. wren11

    wren11 Member

    Messages:
    80
    Likes Received:
    5
    Best Answers:
    3
    Trophy Points:
    43
    #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 Member

    Messages:
    80
    Likes Received:
    5
    Best Answers:
    3
    Trophy Points:
    43
    #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:
    1.  
    2. <?php
    3.         $input = $_GET['input'];//Note to self $input in the name of the search feild
    4.         $terms = explode(" ", $input);
    5.         $query = "SELECT * FROM search WHERE ";
    6.        
    7.         foreach ($terms as $each){
    8.             $i++;
    9.             if ($i == 1)
    10.                 $query .= "keywords LIKE '%$each%' ";
    11.             else
    12.                 $query .= "OR keywords LIKE '%$each%' ";
    13.         }
    14.  
    15. $link = mysql_connect("localhost", "blameyo_xxx", "xxx");
    16. mysql_select_db("blameyo_xxx", $link);
    17.  
    18. $result = mysql_query("SELECT * FROM search", $link);
    19. $num_rows = mysql_num_rows($result);
    20.  
    21. echo "$num_rows Rows\n";
    22. if ($numrows > 0){
    23.  
    24.             while ($row = mysql_fetch_assoc($query)){
    25.                 $id = $row['id'];
    26.                 $title = $row['title'];
    27.                 $description = $row['description'];
    28.                 $keywords = $row['keywords'];
    29.                 $link = $row['link'];
    30.                 echo "<h2><a href='$link'>$title</a></h2>
    31.                $description<br /><br />";
    32.  
    33.             }
    34.            
    35.         }
    36.         else
    37.             echo "No results found for \"<b>$input</b>\"";
    38.        
    39.         // disconnect
    40.         mysql_close();
    41.  
    42.  
    43. ?>
    44.  
    terriblebmx, Apr 28, 2013 IP
  12. wren11

    wren11 Member

    Messages:
    80
    Likes Received:
    5
    Best Answers:
    3
    Trophy Points:
    43
    #12
    PHP:
    1. <?php
    2.  
    3. $input = $_GET['input'];
    4. $terms = explode(" ", $input);
    5. $query = "SELECT * FROM `search` WHERE ";
    6.  
    7.  
    8. $i = 0;
    9.  
    10. foreach ($terms as $each)
    11. {
    12.     if (($i++ % 2) == 0)
    13.         $query .= "`keywords` LIKE '%$each%' ";
    14.     else
    15.         $query .= "OR `keywords` LIKE '%$each%' ";
    16. }
    17.  
    18. $link = mysql_connect("127.0.0.1", "xxx", "xxx");
    19. mysql_select_db("blameyo_xxx", $link);
    20.  
    21. $results = mysql_query($query, $link);
    22.  
    23. if (mysql_num_rows($results) > 0)
    24. {
    25.     while ($row = mysql_fetch_Array($results))
    26.     {
    27.         echo $row['ID'] . "<BR />";
    28.     }
    29. }
    30. else
    31. {
    32.     echo "No Results found.";  
    33. }
    34.  
    35.  
    36. mysql_close($link);
    37.  
    38. ?>
    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 Member

    Messages:
    80
    Likes Received:
    5
    Best Answers:
    3
    Trophy Points:
    43
    #15
    This should be working, Change the db stuff to yours

    PHP:
    1.  
    2. <html xmlns="">
    3.  
    4. <body>
    5.  
    6. <h1 style="color;#09F; font-size;36px;">SEARCH</h1>
    7. <form action="search.php" method="get">
    8. <input type=text name=input size=50>
    9. <input type="submit" value="search" />
    10. </form>
    11.  
    12. <hr/>
    13.  
    14.  
    15. <?php
    16.  
    17. $input = $_GET['input'];
    18.  
    19. if (isset($input))
    20. {
    21.     $terms = explode(" ", $input);
    22.     $query = "SELECT * FROM `search` WHERE ";
    23.  
    24.  
    25.     $i = 0;
    26.  
    27.     foreach ($terms as $each)
    28.     {
    29.     if (($i++ % 2) == 0)
    30.         $query .= "`keywords` LIKE '%$each%' ";
    31.     else
    32.         $query .= "OR `keywords` LIKE '%$each%' ";
    33.     }
    34.  
    35.     $link = mysql_connect("localhost", "tester", "xxxx");
    36.     mysql_select_db("xxx", $link);
    37.  
    38.     $results = mysql_query($query, $link);
    39.  
    40.     if (mysql_num_rows($results) > 0)
    41.     {
    42.         while ($row = mysql_fetch_Array($results))
    43.         {
    44.             $id = $row['id'];
    45.             $title = $row['title'];
    46.             $description = $row['description'];
    47.             $keywords = $row['keywords'];
    48.             $link = $row['link'];
    49.             echo "<h2><a href='$link'>$title</a></h2>$description<br /><br />";
    50.         }
    51.     }
    52.     else
    53.     {
    54.         echo "No Results found.";
    55.     }
    56.  
    57.  
    58.     mysql_close();
    59. }
    60.  
    61. ?>
    62.  
    63.  
    64. </body>
    65. </html>
    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