etsted Posted February 5, 2014 Share Posted February 5, 2014 this script is supposed to let me search by multiple keywords, but only lets me search by one at a time. The error i get when i try to type in several keywords is: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:wampwwwsearch.php on line 32 <h1>search engine</h1> <form action="search.php" method="get"> <input type="text" name="k" size='50' /> <input type="submit" name="submit" value='Search' /> </form> <hr /> <?php include "connect.php"; $k = $_GET['k']; $terms = explode(" ", $k); $query = "SELECT * FROM search WHERE "; foreach($terms as $each) { $i = 0; $i++; if($i == 1) { $query .= "keywords LIKE '%$each%' "; } else { $query .= "OR keywords LIKE '%$each%' "; } } $result = mysqli_query($con, $query); $numrows = mysqli_num_rows($result); if($numrows > 0) { while($row = mysqli_fetch_array($result)) { $id = $row['id']; $title = $row['title']; $description = $row['description']; $keywords = $row['keywords']; $link = $row['link']; if($k == "") { echo ""; } else { echo "<h2><a href='$link'>$title</a></h2> $description"; } } } else { echo "no result on $k"; } Link to comment Share on other sites More sharing options...
etsted Posted February 5, 2014 Author Share Posted February 5, 2014 this time i used a mysqli_error() to check for errors on a new script, and got this message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'description LIKE '%w%'' at line 1 I used the following search engine code below. <h1>search engine</h1> <form action="search.php" method="get"> <input type="text" name="k" size='50' /> <input type="submit" name="submit" value='Search' /> </form> <hr /> <?php $k = $_GET['k']; $terms = explode(" ", $k); $query = "SELECT * FROM videos WHERE "; foreach($terms as $each) { $i = 0; $i++; if($i == 1) { $query .= "description LIKE '%$each%' "; } else { $query .= "OR description LIKE '%$each%' "; } } include "connect.php"; $result = mysqli_query($con, $query) or die(mysqli_error($con)); $numrows = mysqli_num_rows($result); if($numrows > 0) { while($row = mysqli_fetch_array($result)) { $id = $row['id']; $title = $row['title']; $url = $row['url']; $description = $row['description']; if($k == "") { echo ""; } else { echo "<h3><a href='$url'>$title</a></h3> $description"; } } } else { echo "no results on $k"; } ?> Link to comment Share on other sites More sharing options...
justsomeguy Posted February 5, 2014 Share Posted February 5, 2014 foreach($terms as $each) { $i = 0; $i++; if($i == 1) { $i is always going to be 1. Link to comment Share on other sites More sharing options...
etsted Posted February 6, 2014 Author Share Posted February 6, 2014 how do i fix that? Link to comment Share on other sites More sharing options...
Don E Posted February 6, 2014 Share Posted February 6, 2014 You can move $i = 0 from the foreach loop and put it right before it. Link to comment Share on other sites More sharing options...
etsted Posted February 6, 2014 Author Share Posted February 6, 2014 it didnt work. now i get the error message all the time. Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OR description LIKE '%%'' at line 1 Link to comment Share on other sites More sharing options...
thescientist Posted February 6, 2014 Share Posted February 6, 2014 (edited) sounds like you don't have anything in your LIKE clause. try printing the full query and debugging your loop to make you're code is doing what you think it's doing. if you are having problems, please post the updated / changed code. Edited February 6, 2014 by thescientist Link to comment Share on other sites More sharing options...
justsomeguy Posted February 6, 2014 Share Posted February 6, 2014 Print out the query you've built. You should also really be using prepared statements to protect against SQL injections. Link to comment Share on other sites More sharing options...
etsted Posted February 6, 2014 Author Share Posted February 6, 2014 new code: Error: SELECT * FROM videos WHERE OR description LIKE '%%' You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OR description LIKE '%%'' at line 1 <h1>Search:</h1> <form action="search.php" method="get" class="search"> <input type="text" name="k" size='50' /> <input type="submit" name="submit" value='Search' /> </form> <?php $username = $_SESSION['username']; if(isset($username)) { include "navigator/navigator_login.php"; } else { include "navigator/navigator.php"; } ?> <hr /> <?php include "connect.php"; $k = mysqli_real_escape_string($con, htmlentities($_GET['k'])); $terms = explode(" ", $k); $query = "SELECT * FROM videos WHERE "; $i = 0; foreach($terms as $each) { $i++; if($i == 0) { $query .= "description LIKE '%$each%' "; } else { $query .= "OR description LIKE '%$each%' "; } } print_r($query); echo "<br />"; $result = mysqli_query($con, $query) or die(mysqli_error($con)); $numrows = mysqli_num_rows($result); if($numrows > 0) { while($row = mysqli_fetch_array($result)) { $id = $row['id']; $title = $row['title']; $url = $row['url']; $description = $row['description']; if($k == "") { echo ""; } else { echo "<h3><a href='$url'>$title</a></h3> $description"; } } } else { echo "no results on $k"; } mysqli_close($con); ?> Link to comment Share on other sites More sharing options...
justsomeguy Posted February 6, 2014 Share Posted February 6, 2014 Well, look at the query. I hope you can see what the problem is. Look at your code that builds the query, is $i ever going to be 0? Link to comment Share on other sites More sharing options...
etsted Posted February 6, 2014 Author Share Posted February 6, 2014 okey so i think its fixed, but every time i add only blank space, all the data from my database is displayed. how can i prevent that? new code: <form action="search.php" method="get" class="search"> <input type="text" name="k" size='50' /> <input type="submit" name="submit" value='Search' /> </form> <?php $username = $_SESSION['username']; if(isset($username)) { include "navigator/navigator_login.php"; } else { include "navigator/navigator.php"; } ?> <hr /> <?php include "connect.php"; $k = mysqli_real_escape_string($con, htmlentities($_GET['k'])); $terms = explode(" ", $k); $query = "SELECT * FROM videos WHERE "; $i = 0; foreach($terms as $each) { $i++; if($i == 1) { $query .= "description LIKE '%$each%' "; } else { $query .= "OR description LIKE '%$each%' "; } } $result = mysqli_query($con, $query) or die(mysqli_error($con)); $numrows = mysqli_num_rows($result); if($numrows > 0) { while($row = mysqli_fetch_array($result)) { $id = $row['id']; $title = $row['title']; $url = $row['url']; $description = $row['description']; if($k == "") { echo ""; } else { echo "<h3><a href='$url'>$title</a></h3> $description"; } } } else { echo "no results on $k"; } mysqli_close($con); ?> Link to comment Share on other sites More sharing options...
justsomeguy Posted February 6, 2014 Share Posted February 6, 2014 You can use trim to strip whitespace from a variable: http://www.php.net/manual/en/function.trim.php And you can also add a check to test if the search variable is empty and show an error message or whatever you want to show. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now