Renato Posted January 5, 2015 Share Posted January 5, 2015 I'm creating search box, to search users on my database this is the form <form action='' method='get' > <input type='text' name='search' /> <input type='submit' value='search'/> <br/> </form> php <?php $search = $_GET['search']; $terms = explode( " ", $search); $query = "SELECT * FROM login WHERE"; foreach ($terms as $each) { $i++; if ($i==0) $query .= "username like '%$each%' "; else $query .= "OR username like '%$each%' "; } mysql_connect("localhost", "renatov_admin", "password1!") or die (mysql_error()); mysql_select_db("renatov_database") or die ("could not find bd!"); $squery = mysql_query($query); $numrows = mysql_num_rows($query); if ($numrows > 0){ while($row = mysql_fetch_assoc($query)){ $username = $row['username']; $nationality = $row['nationality']; $age = $row['age']; $search = $row['search']; echo "<h2><a href='$link'>$username</a> </h2>"; } } else echo "No results found for \"<b>$search</b> \""; mysql_close(); ?> it comes with error: Warning: mysql_num_rows() expects parameter 1 to be resource, string given Is always "No result found for" Can someone tell me where I'm doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/293697-php-search-box/ Share on other sites More sharing options...
requinix Posted January 5, 2015 Share Posted January 5, 2015 $query is just the string SQL query. You want $squery, which is the actual result of executing the query (or at least trying to). Quote Link to comment https://forums.phpfreaks.com/topic/293697-php-search-box/#findComment-1501870 Share on other sites More sharing options...
Renato Posted January 5, 2015 Author Share Posted January 5, 2015 I've try to, it didn't work. When I search it shows on URL, the word that has been input to the search box. Quote Link to comment https://forums.phpfreaks.com/topic/293697-php-search-box/#findComment-1501871 Share on other sites More sharing options...
CroNiX Posted January 6, 2015 Share Posted January 6, 2015 Requinix is saying that $query here, needs to be $squery: $numrows = mysql_num_rows($query); Quote Link to comment https://forums.phpfreaks.com/topic/293697-php-search-box/#findComment-1501872 Share on other sites More sharing options...
Renato Posted January 6, 2015 Author Share Posted January 6, 2015 Requinix is saying that $query here, needs to be $squery: $numrows = mysql_num_rows($query); I have change to $squery; but i still get error -Warning: mysql_num_rows() expects parameter 1 to be resource, boolean And still get no results when search for username. Quote Link to comment https://forums.phpfreaks.com/topic/293697-php-search-box/#findComment-1501873 Share on other sites More sharing options...
CroNiX Posted January 6, 2015 Share Posted January 6, 2015 (edited) Then most likely your query is failing, but you never check for that and just assume it runs . See how they do it in http://php.net/manual/en/function.mysql-query.php in the first example labeled "Example #1 invalid query" My guess is because you don't have a space after the WHERE and when you add to the query. As a side note, mysql is deprecated and will be removed from PHP in the future. You should be using PDO or mysqli if you don't want to have to rewrite all of this for newer upcoming versions of PHP. You're not doing yourself any favors by using something that is already deprecated. Edited January 6, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/293697-php-search-box/#findComment-1501874 Share on other sites More sharing options...
hansford Posted January 6, 2015 Share Posted January 6, 2015 Do yourself a favor to save frustration - echo out your query before hitting the database. echo $query; exit(); $squery = mysql_query($query); $numrows = mysql_num_rows($squery); Quote Link to comment https://forums.phpfreaks.com/topic/293697-php-search-box/#findComment-1501878 Share on other sites More sharing options...
ginerjm Posted January 6, 2015 Share Posted January 6, 2015 At the least you need a space after WHERE Quote Link to comment https://forums.phpfreaks.com/topic/293697-php-search-box/#findComment-1501879 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.