Presto-X Posted April 14, 2008 Share Posted April 14, 2008 Hello guys, I'm working on a very basic page, I want to search one table in my database I was thinking this was going to be simple but I keep getting this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /volume1/web/mangos/templates/search.php on line 11 This is my code <form method="POST" action="search.php"> <input type="text" name="search" size=25 maxlength=25> <input type="Submit" name="Submit" value="Submit"> </form> <?PHP mysql_connect("localhost", "XXXXX", "XXXXX"); mysql_select_db("XXXXX"); $search=$_POST["search"]; $result = mysql_query("SELECT name FROM item_tempate WHERE name LIKE '%$search%'"); while($r=mysql_fetch_array($result)) { $name=$r["name"]; echo "$name<br>"; } ?> What am I missing? Link to comment https://forums.phpfreaks.com/topic/101072-solved-simple-search-form-giving-warning/ Share on other sites More sharing options...
GingerRobot Posted April 14, 2008 Share Posted April 14, 2008 This means that either your query is failing, or there are no rows returned. Try modifying your query to this: <form method="POST" action="search.php"> <input type="text" name="search" size=25 maxlength=25> <input type="Submit" name="Submit" value="Submit"> </form> <?php mysql_connect("localhost", "XXXXX", "XXXXX"); mysql_select_db("XXXXX"); if(isset($_POST['submit'])){ $search=$_POST["search"]; $result = mysql_query("SELECT name FROM item_tempate WHERE name LIKE '%$search%'") or die(mysql_error()); $num = mysql_num_rows($result); if($result > 0){ while($r=mysql_fetch_array($result)) { $name=$r["name"]; echo "$name<br>"; } }else{ echo 'No matching items found!'; } } ?> You'll notice i've also added something to ensure the query is only perfomed once the search button has been pressed. Link to comment https://forums.phpfreaks.com/topic/101072-solved-simple-search-form-giving-warning/#findComment-516818 Share on other sites More sharing options...
Presto-X Posted April 14, 2008 Author Share Posted April 14, 2008 Hey Ben, Thanks for the reply, that took care of the warning, but it does not display anything even if it did not find anything? I also found a misspelling on my part for the table name "item_tempate" should be "item_template". But it's not returning any results. Is there any tag that I can place in here to make sure it’s connecting to the database and to the table ok? <form method="POST" action="search.php"> <input type="text" name="search" size=25 maxlength=25> <input type="Submit" name="Submit" value="Submit"> </form> <?php mysql_connect("localhost", "XXXXX", "XXXXX"); mysql_select_db("XXXXX"); if(isset($_POST['submit'])){ $search=$_POST["search"]; $result = mysql_query("SELECT name FROM item_template WHERE name LIKE '%$search%'") or die(mysql_error()); $num = mysql_num_rows($result); if($result > 0){ while($row=mysql_fetch_array($result)) { $name=$row["name"]; echo "$name<br>"; } }else{ echo 'No matching items found!'; } } ?> Link to comment https://forums.phpfreaks.com/topic/101072-solved-simple-search-form-giving-warning/#findComment-516845 Share on other sites More sharing options...
taith Posted April 14, 2008 Share Posted April 14, 2008 change $num = mysql_num_rows($result); if($result > 0){ to if(mysql_num_rows($result) > 0){ Link to comment https://forums.phpfreaks.com/topic/101072-solved-simple-search-form-giving-warning/#findComment-516849 Share on other sites More sharing options...
GingerRobot Posted April 14, 2008 Share Posted April 14, 2008 Sorry, that would be my fault - didn't notice you'd capitalized the first letter of the name of the submit button - hence the if(isset()) was always failing. Try: <form method="POST" action="search.php"> <input type="text" name="search" size=25 maxlength=25> <input type="Submit" name="Submit" value="Submit"> </form> <?php mysql_connect("localhost", "XXXXX", "XXXXX"); mysql_select_db("XXXXX"); if(isset($_POST['Submit'])){ $search=$_POST["search"]; $result = mysql_query("SELECT name FROM item_template WHERE name LIKE '%$search%'") or die(mysql_error()); if(mysql_num_rows($result) > 0){ while($r=mysql_fetch_array($result)) { $name=$r["name"]; echo "$name<br>"; } }else{ echo 'No matching items found!'; } } ?> change $num = mysql_num_rows($result); if($result > 0){ to if(mysql_num_rows($result) > 0){ Indeed - not sure where that came from! Link to comment https://forums.phpfreaks.com/topic/101072-solved-simple-search-form-giving-warning/#findComment-516852 Share on other sites More sharing options...
Presto-X Posted April 14, 2008 Author Share Posted April 14, 2008 humm, still nothing :-\ <form method="POST" action="search.php"> <input type="text" name="search" size=25 maxlength=25> <input type="Submit" name="Submit" value="Submit"> </form> <?php mysql_connect("localhost", "XXXX", "XXXX"); mysql_select_db("mangos"); if(isset($_POST['submit'])){ $search=$_POST["search"]; $result = mysql_query("SELECT name FROM item_template WHERE name LIKE '%$search%'") or die(mysql_error()); if(mysql_num_rows($result) > 0){ while($row=mysql_fetch_array($result)) { $name=$row["name"]; echo "$name<br>"; } }else{ echo 'No matching items found!'; } } ?> Link to comment https://forums.phpfreaks.com/topic/101072-solved-simple-search-form-giving-warning/#findComment-516864 Share on other sites More sharing options...
GingerRobot Posted April 14, 2008 Share Posted April 14, 2008 You didn't make my change. Change this line: if(isset($_POST['submit'])){ To: if(isset($_POST['Submit'])){ Note the capital S. Link to comment https://forums.phpfreaks.com/topic/101072-solved-simple-search-form-giving-warning/#findComment-516879 Share on other sites More sharing options...
Presto-X Posted April 14, 2008 Author Share Posted April 14, 2008 Nice that capital S did the trick, Thanks Ben. This is the working code in case someone else finds this useful <form method="POST" action="search.php"> <input type="text" name="search" size=25 maxlength=25> <input type="Submit" name="Submit" value="Submit"> </form> <?php mysql_connect("localhost", "USERNAME", "PASSWORD"); mysql_select_db("DATABASE"); if(isset($_POST['Submit'])){ $search=$_POST["search"]; $result = mysql_query("SELECT name FROM item_template WHERE name LIKE '%$search%'") or die(mysql_error()); if(mysql_num_rows($result) > 0){ while($row=mysql_fetch_array($result)) { $name=$row["name"]; echo "$name<br>"; } }else{ echo 'No matching items found!'; } } ?> Link to comment https://forums.phpfreaks.com/topic/101072-solved-simple-search-form-giving-warning/#findComment-516887 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.