mysql query formating

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RafaelT
Forum Newbie
Posts: 16
Joined: Mon Aug 10, 2009 10:31 am

mysql query formating

Post 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
User avatar
dheeraj
Forum Commoner
Posts: 40
Joined: Fri Feb 06, 2009 11:54 am

Re: mysql query formating

Post 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.....
RafaelT
Forum Newbie
Posts: 16
Joined: Mon Aug 10, 2009 10:31 am

Re: mysql query formating

Post 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
RafaelT
Forum Newbie
Posts: 16
Joined: Mon Aug 10, 2009 10:31 am

Re: mysql query formating

Post by RafaelT »

FIXED
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: mysql query formating

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
RafaelT
Forum Newbie
Posts: 16
Joined: Mon Aug 10, 2009 10:31 am

Re: mysql query formating

Post 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
Post Reply