Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search w/ variables

Status
Not open for further replies.

cs12xge

Programmer
Dec 3, 2000
8
US
Hey Everybody,
My name is Henry, I'm working on a program that uses mysql, and what I'm trying to do is do a query inside php

$result = mysql_query("SELECT * FROM keyTable WHERE
keyword = $tok");

$tok being a variable that contains a token from a long string like

("earth fire wind"),

and I would like to be able to search for each of the words from the string one at a time.


Trouble being mysql doesn't take in variables when the run a WHERE clause in select, it has to be an actual string in double quotes like "i_am_a_string".

is there anyway that I can get around this?

would appreciate it.

Henry
 
Yes .

$SQL = "SELECT * FROM keytable WHERE
keywork = '$tok'";

$result = mysql_query($SQL)

to search for a word in the $tok variable , you can use
LIKE

ie.
$SQL = "SELECT * FROM keyTable WHERE
keywork LIKE '$tok'";

Cheers

Ken
admin@mysqlwebring.com
 
Hi Ken,

thanks for the quick reply!!! i thought I never get it in a year...

Well, I tried both method with the two ' and it seemed to be looking for the exact string "$tok" instead of the value inside the variable.
then I tried it without the ' and it still could not query. I just want to make sure I asked the correct question..

I have a $str which has a string like "earth fire wind"

and then in a while loop i use strtok just to get "earth", "fire", and "wind" stored inside $tok one at a time, and I'm attempting to search for

"earth", "fire", and "wind" one at a time in the loop.

the LIKE clause seems not be doing the magic...

this is what I did..

$findPagesWithKeywords = "SELECT * FROM keyTable WHERE keyword LIKE $tok";

or

$findPagesWithKeywords = "SELECT * FROM keyTable WHERE keyword LIKE '$tok'";

and then

$result = mysql_query($findPagesWithKeywords, $conn)
or die ("could not query");

the former case displayed an error, the later case gave me back empty results, and I believed that the later case was actually searching for "$str" instead of $str.

if I'm not doing this wrong, would you have any other suggestions?

very much appreciate your help!!!

Henry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top