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!

query using variables 1

Status
Not open for further replies.

cs12xge

Programmer
Dec 3, 2000
8
US
Hi Everybody,

this is actually more of a mysql question but i'm working with both that and php.

My name is Henry, what I'm trying to do is do a query inside php

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

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

$conn is the connection I established,

$str is a variable that is like "earth fire wind"

and I used strtok to parsed it and in a while loop have
each of those words, "earth", "fire", and "wind" stored in a variable called $tok.

my program depend on being able to search for each of these words from the string one at a time, but I have now learned that SELECT usually works with a set string in double quotes like "i_am_a_string".

I also tried doing it with the LIKE under WHERE...

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

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

it doeosn't seem to do the magic either, keeps "dying".

is there anyway that I can get around this?

Any suggestion would be greatly appreciated.

Henry

p.s. I posted a similar message in the mysql forum, Just trying to get help as soon as I can. no offense to anyone :)
 
$findPagesWithKeywords = "SELECT * FROM keyTable WHERE
keyword LIKE \"$tok\"";

but i'm not that sure as i'm new to php
 
HI iza,
I tried it out but i don't think that was the way to go, I might have done it wrong, but if you put them in double quotes wouldn't it search for the exact string "$str" instead of the value insde the variable? Thanks for your help anyways :)

Henry
 
Actually, what you want to do is use percent symbols around the search string.

i.e.
$sql = "SELECT * FROM keyTable WHERE keyword LIKE '%$str%'";

The percent signs are wildcards matching any character or border.

That'll do ya.

brendanc@icehouse.net
 
i didn't read carefully enough and i didn't get it was a sql problem - sophisticate is right !!! and yes, cs12xge, i guess this would have queried for $str :-/ next time i'll answer slowlier ;]]]
 
Thanks sophisticate,
Can you believe that I was stuck on this for 3 days?

and iza, thanks for your input too.

Henry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top