Jump to content

[SOLVED] PHP script error


Exc.BluePhoenix

Recommended Posts

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>";
}
?>

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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>";
}
?> 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";
}
?> 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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>";
}
?> 

Link to comment
Share on other sites

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>";
}
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.