Page 1 of 1

mysql query formating

Posted: Wed Aug 12, 2009 3:14 am
by RafaelT
Hi, I am trying to get the query below to work and I am having no luck. I have tried writing it 50 different ways and nothing works.

All I end up with is "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in..."

I know what is below is not close to correct, I just left $type and $search there to show where they go.

The $type and $search are screwing it up, the query works fine if you were to substitute "name" for "$type" and "John Doe" for "$search"

Can anyone re write that so it actually works?

Code: Select all

 
$type = $_POST[type];
$search = $_POST[search];
 
$query = mysql_query("SELECT id, name, num, attend, phone, email, address FROM Invites WHERE $type LIKE $search");
 
Thank you

Re: mysql query formating

Posted: Wed Aug 12, 2009 3:21 am
by dheeraj
try this...

Code: Select all

$query = mysql_query("SELECT id, name, num, attend, phone, email, address FROM Invites WHERE ".$type." LIKE "'.$search."'");
may be it will work.....

Re: mysql query formating

Posted: Wed Aug 12, 2009 3:32 am
by RafaelT
Thank you for the reply

I tried that and I got Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

I tried changing

Code: Select all

$query = mysql_query("SELECT id, name, num, attend, phone, email, address FROM Invites WHERE ".$type." LIKE "'.$search."'");
 
to

Code: Select all

$query = mysql_query("SELECT id, name, num, attend, phone, email, address FROM Invites WHERE ".$type." LIKE ".$search."");
(look at the search part, maybe thats what you meant?)
But that got me back to my original error.

Also something I ommited in my first post is $search must have "%" around it "%$search%"

Thanks

Re: mysql query formating

Posted: Wed Aug 12, 2009 4:08 am
by RafaelT
FIXED

Re: mysql query formating

Posted: Wed Aug 12, 2009 10:03 am
by pickle
Please post your solution so others that are having this problem in the future can be helped.

I'm guessing you fixed the problem by putting quotes around $type and $search, as they would be strings in the query & need to be delimited as such.

Re: mysql query formating

Posted: Wed Aug 12, 2009 11:17 am
by RafaelT
Well my solution was actually fixing errors in other parts of my code. I assume now any standard way of putting those in the line would work fine.

What I am using now, which I am sure is still not technically correct, but works, is..

Code: Select all

 
$query = mysql_query("SELECT id, name, num, attend, phone, email, address FROM Invites WHERE $type LIKE '%$search%'");
 
Thanks