Exc.BluePhoenix Posted January 12, 2008 Share Posted January 12, 2008 Hello I am trying a simple search script by following a tutorial. However I get the following error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\Program_Files\wamp\www\searchresults.php on line 18. After that I read around how to try and fix it, and to my dismay nothing really worked, or more so nothing really fitted my example. Afterwards I added the line "or die(mysql_error())" to give me the problem. The result of this says "Query was empty", of course by knowing English I understand that the Query is empty but I don't know how to make it not empty or how to not give me the error. Can anyone point out to me the error in the script please, thank you. <?php $db = mysql_connect("localhost","","password"); mysql_select_db("exampledb", $db); $guery = "SELECT shops.name, shops.phone, shops.email, shops.website FROM shops WHERE name LIKE '%".$name."%'"; $result = mysql_query($query) or die( mysql_error() ); while ($record = mysql_fetch_assoc($result)){ while (list($fieldname, $fieldvalue) = each($record)){ echo $fieldname.":<B>".$fieldvalue."</B><BR>"; } echo "<BR>"; } ?> Quote Link to comment Share on other sites More sharing options...
mrdamien Posted January 12, 2008 Share Posted January 12, 2008 $guery = "SELECT shops.name, shops.phone, shops.email, shops.website FROM shops WHERE name LIKE '%".$name."%'"; $result = mysql_query($query) or die( mysql_error() ); You have a typo: $guery != $query G/Q $query = "SELECT shops.name, shops.phone, shops.email, shops.website FROM shops WHERE name LIKE '%".$name."%'"; Use that instead Quote Link to comment Share on other sites More sharing options...
Exc.BluePhoenix Posted January 12, 2008 Author Share Posted January 12, 2008 Well thank you very much for the reply and the answer. It works now, I cannot believe that I spend 3 hours over a typo. Thank you because I was never going to catch that. However I now find myself with one more question. This is supposed to be a search script. I have a very simple form that goes with this. Now I wanted to see if its working properly before I move on. So I type in the search box the word "records" what this script is supposed to do is give back information on all the shops that match that name. However, when I do it, it sends back all the shops. Can anyone point out what might be the reason for this? Here is the code again. <?php $db = mysql_connect("localhost","","inuyasha"); mysql_select_db("exampledb", $db); $query = "SELECT shops.name, shops.phone, shops.email, shops.website FROM shops WHERE name LIKE '%".$name."%'"; $result = mysql_query($query) or die( mysql_error() ); while ($record = mysql_fetch_assoc($result)){ while(list($fieldname, $fieldvalue) = each($record)){ echo $fieldname.":<B>".$fieldvalue."</B><BR>"; } echo "<BR>"; } ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted January 12, 2008 Share Posted January 12, 2008 echo $query before you run the mysql_query command and make sure it looks like it should. Maybe $name is empty so it returns everything (%%) Quote Link to comment Share on other sites More sharing options...
Exc.BluePhoenix Posted January 12, 2008 Author Share Posted January 12, 2008 When you say echo $query before the mysql_query command and make sure it looks like it should, what do you mean? I dont understand what should it look like? Still i did it and it just adds the string. Furthermore when you say maybe $name is empty, do you mean in the db? if so its not; I just check with phpmyadmin. I'm sorry for the trouble. Quote Link to comment Share on other sites More sharing options...
revraz Posted January 12, 2008 Share Posted January 12, 2008 Copy and paste what it echos. <?php $db = mysql_connect("localhost","","inuyasha"); mysql_select_db("exampledb", $db); $query = "SELECT shops.name, shops.phone, shops.email, shops.website FROM shops WHERE name LIKE '%".$name."%'"; echo $query; //add this exit; //add this $result = mysql_query($query) or die( mysql_error() ); while ($record = mysql_fetch_assoc($result)){ while(list($fieldname, $fieldvalue) = each($record)){ echo $fieldname.":<B>".$fieldvalue."</B><BR>"; } echo "<BR>"; } ?> Quote Link to comment Share on other sites More sharing options...
Exc.BluePhoenix Posted January 12, 2008 Author Share Posted January 12, 2008 I think that I get what you meant by the name been empty now. Here is the return. SELECT shops.name, shops.phone, shops.email, shops.website FROM shops WHERE name LIKE '%%' '%%' there is nothing there is that what you meant before? Also anyway of fixing this that you may offer, or pointing the problem? Thank you again, and also for your previous replies. Quote Link to comment Share on other sites More sharing options...
revraz Posted January 12, 2008 Share Posted January 12, 2008 So that means $name is empty. With the code you posted, it's not shown where you set it. Quote Link to comment Share on other sites More sharing options...
Exc.BluePhoenix Posted January 12, 2008 Author Share Posted January 12, 2008 That is all my PHP code however, maybe the form html will help? <head> <title>Record Shops Search</title> </head> <body> <H2> Search for a shop</H2> <BR> <FORM ACTION="searchresults.php" METHOD="POST"> Please enter the name, or part of the name, of the shop you are seeking: <BR> <INPUT NAME="name" TYPE=TEXT /> <BR> <INPUT TYPE=SUBMIT VALUE="search"> </FORM> </body> </html> If that does not help, then its fine and thanks for your help, really nice of you. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted January 12, 2008 Share Posted January 12, 2008 <?php $db = mysql_connect("localhost","","inuyasha"); mysql_select_db("exampledb", $db); $query = "SELECT shops.name, shops.phone, shops.email, shops.website FROM shops WHERE name LIKE '%".$_POST['name']."%'"; $result = mysql_query($query) or die( mysql_error() ); while ($record = mysql_fetch_assoc($result)){ while(list($fieldname, $fieldvalue) = each($record)){ echo $fieldname.":<B>".$fieldvalue."</B><BR>"; } echo "<BR>"; } ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted January 12, 2008 Share Posted January 12, 2008 Or preferred <?php $db = mysql_connect("localhost","","inuyasha"); mysql_select_db("exampledb", $db); $name = mysql_real_escape_string($_POST['name']); $query = "SELECT shops.name, shops.phone, shops.email, shops.website FROM shops WHERE name LIKE '%".$name."%'"; echo $query; //dont forget to remove this exit; //dont forget to remove this $result = mysql_query($query) or die( mysql_error() ); while ($record = mysql_fetch_assoc($result)){ while(list($fieldname, $fieldvalue) = each($record)){ echo $fieldname.":<B>".$fieldvalue."</B><BR>"; } echo "<BR>"; } ?> Quote Link to comment Share on other sites More sharing options...
Exc.BluePhoenix Posted January 12, 2008 Author Share Posted January 12, 2008 LOL revraz thanks for that last one, first thing I was not going to do. Thank you Ken2k7 that worked LIKE A CHARM, I appreciated, same goes to revraz and mrdamien. Thanks a lot!!! Quote Link to comment Share on other sites More sharing options...
revraz Posted January 12, 2008 Share Posted January 12, 2008 I also added this line in there so you dont let bad info come in $name = mysql_real_escape_string($_POST['name']); Quote Link to comment 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.