Welcome to Dream.In.Code
Become an Expert!

Join 150,018 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,471 people online right now. Registration is fast and FREE... Join Now!




mysql search problem

 
Reply to this topicStart new topic

mysql search problem

Kempy535
23 Nov, 2006 - 08:31 AM
Post #1

New D.I.C Head
*

Joined: 19 Nov, 2006
Posts: 10


My Contributions
Hi Im trying to make a search feature for a site in php that uses a mysql data base. I want to be able to search the whole data base, I have 11 fields, the search word entered in via a form. So far I have the following codes,
this is the code in the search.php page which is the return page from the form,
CODE
<?php

$searchf = $_POST["searchf"];

function testsearch()
{
$result = mysql_query("SELECT * FROM stocklist LIKE $searchf");
while($row = mysql_fetch_array($result))
  {
  echo $row['make']."  ".$row['model']."  ".$row['title'] ."  ".$row['price'];
  echo "<br />";
  }
}
  testsearch();
?>


This is the form code that on the ;
CODE
<form action="search.php" target="mainFrame" method="post">
Search: <input type="text" name="searchf" />
<input type="submit" />
</form>


The main problem I am having at the moment is i get this error message,
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\htdocs\Rising sun\search.php on line 20

I cant figure out why it wont work its sending me mad.
Firstly will the code work? Is it just about changing the mysql_fetch_array for another command? I have other mysql querys running and they work fine. Any help or idea will be hugly appresated.
Thanks.
User is offlineProfile CardPM
+Quote Post

snoj
RE: Mysql Search Problem
23 Nov, 2006 - 08:43 AM
Post #2

Fell off the face of the earth
Group Icon

Joined: 31 Mar, 2003
Posts: 3,325



Thanked: 9 times
Dream Kudos: 750
My Contributions
you have a SQL syntax error. It looks like you need ' marks around $searchf so mysql knows and treats it like a string.
User is offlineProfile CardPM
+Quote Post

Kempy535
RE: Mysql Search Problem
23 Nov, 2006 - 09:55 AM
Post #3

New D.I.C Head
*

Joined: 19 Nov, 2006
Posts: 10


My Contributions
I just tried that and I still get the same error code mad.gif
User is offlineProfile CardPM
+Quote Post

Skinnyarms
RE: Mysql Search Problem
23 Nov, 2006 - 07:03 PM
Post #4

New D.I.C Head
*

Joined: 18 Nov, 2006
Posts: 32


My Contributions
Maybe I'm missing something, but I don't see a WHERE clause...

Your query should look something like

CODE

$result = mysql_query("SELECT   *
FROM      stocklist
WHERE    (your_column_name_here) LIKE '$your_value_here'");


Like the above poster says, the LIKE clause is intended for strings, so you'll need single quotes around your php variable.

Also, I'm no php expert, but from what I can tell your $searchf contains all your post variables, so it seems to me like you'll want to loop through them, i don't know the syntax for php, so I'll kind of pseudocode it for you...

CODE

$myString = "SELECT   * FROM stocklist";

for($i = 1, $i < Length($searchf), i++) {
    $myString += "WHERE ($i LIKE '$evaluated_value(i)')";
}

$result = mysql_query(myString);
");


Hope that helps!
User is offlineProfile CardPM
+Quote Post

snoj
RE: Mysql Search Problem
23 Nov, 2006 - 07:17 PM
Post #5

Fell off the face of the earth
Group Icon

Joined: 31 Mar, 2003
Posts: 3,325



Thanked: 9 times
Dream Kudos: 750
My Contributions
That's still wrong as you'd have more than one WHERE. Though skinnyarms is right, you are missing WHERE.
User is offlineProfile CardPM
+Quote Post

Kempy535
RE: Mysql Search Problem
24 Nov, 2006 - 03:39 AM
Post #6

New D.I.C Head
*

Joined: 19 Nov, 2006
Posts: 10


My Contributions
I want it to search all the colloums at once not ust one?
So can i use the * instead of (your_column_name_here) in the code below?

CODE
$result = mysql_query("SELECT   *
FROM      stocklist
WHERE    (your_column_name_here) LIKE '$your_value_here'");

User is offlineProfile CardPM
+Quote Post

Skinnyarms
RE: Mysql Search Problem
24 Nov, 2006 - 05:47 AM
Post #7

New D.I.C Head
*

Joined: 18 Nov, 2006
Posts: 32


My Contributions
QUOTE(hotsnoj @ 23 Nov, 2006 - 08:17 PM) *

That's still wrong as you'd have more than one WHERE. Though skinnyarms is right, you are missing WHERE.



ACK! You're right! Corrected:

CODE

$myString = "SELECT   * FROM stocklist WHERE 1 = 1";

for($i = 1, $i < Length($searchf), i++) {
    $myString += "AND ($i LIKE '$evaluated_value(i)')";
}

$result = mysql_query(myString);
");


and as for searching all at once, there's no way I know of to do that with out looping through the post variables.

Also worth noting that the LIKE clause only works for strings, but if your not doing any sort of wild card characters, you might as well use the equal sign. (WHERE myKey = 'myValue')
User is offlineProfile CardPM
+Quote Post

Kempy535
RE: Mysql Search Problem
26 Nov, 2006 - 02:40 PM
Post #8

New D.I.C Head
*

Joined: 19 Nov, 2006
Posts: 10


My Contributions
Hi guys i got it to work in the end. Thanks for you help. Heres how I did it.


CODE
<?php
$searchf = $_POST["searchf"];
$sql = "SELECT * FROM stocklist WHERE make = '$searchf' OR model = '$searchf' OR type = '$searchf'";

$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result))
  {
  echo $row['make']."&nbsp;&nbsp;".$row['model']."&nbsp;&nbsp;".$row['title'] ."&nbsp;&nbsp;".$row['price'];
  echo "<br />";
  }
?>


Only problem is I cant get it to work with the wild cards % sad.gif

This post has been edited by Kempy535: 26 Nov, 2006 - 02:41 PM
User is offlineProfile CardPM
+Quote Post

snoj
RE: Mysql Search Problem
26 Nov, 2006 - 04:02 PM
Post #9

Fell off the face of the earth
Group Icon

Joined: 31 Mar, 2003
Posts: 3,325



Thanked: 9 times
Dream Kudos: 750
My Contributions
IIRC, the wildcard only works with LIKE.
User is offlineProfile CardPM
+Quote Post

Kempy535
RE: Mysql Search Problem
27 Nov, 2006 - 02:41 AM
Post #10

New D.I.C Head
*

Joined: 19 Nov, 2006
Posts: 10


My Contributions
Cheers it all working as it should now. Thanks for your help.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:08PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month