We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,886 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

use of like in mysql and php

I am first time using like in mysql .but it is giving error.my query with like is

$user_query="SELECT * FROM rt_user WHERE rt_user_username LIKE %".$_GET['term']."% OR rt_user_name LIKE %".$_GET['term']."% OR rt_user_description LIKE %".$_GET['term']."% LIMIT 10";

it is giving syntax error.

3
Contributors
2
Replies
1 Day
Discussion Span
2 Months Ago
Last Updated
20
Views
daniel36
Junior Poster
188 posts since Nov 2011
Reputation Points: 5
Solved Threads: 3
Skill Endorsements: 0

Do not insert $_GET or $_POST vars directly into SQL -you need to sanitise.

$term = mysql_real_escape_string($_GET['term']);

$user_query="SELECT * FROM rt_user WHERE rt_user_username LIKE '%$term%' OR rt_user_name LIKE '%$term%' OR rt_user_description LIKE '%$term%' LIMIT 10";
diafol
Keep Smiling
Moderator
10,644 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,509
Skill Endorsements: 57

Try wrapping the LIKE arguments in single quotes:

$user_query="SELECT * FROM rt_user WHERE rt_user_username LIKE '%".$_GET['term']."%' OR rt_user_name LIKE '%".$_GET['term']."%' OR rt_user_description LIKE '%".$_GET['term']."%' LIMIT 10";

Notice the single quotes around the LIKE strings?

You should definitely take diafol's advice and sanitize the input instead of just directly injecting the $_GET values into the query.

Also, I cannot be sure without seeing your database structure, but it looks like where you say rt_user_username, you might really mean to say rt_user.username, or even more simply, just username. What you have (and maybe this is what you want, like I said, I cannot be sure without seeing the schema) is looking for a column named rt_user_username in the rt_user table. What I wrote, rt_user.username, means a column named username in the table named rt_user. Just thought I'd mention that in case it turns out to be another issue.

dcdruck
Junior Poster in Training
89 posts since Jul 2009
Reputation Points: 21
Solved Threads: 20
Skill Endorsements: 0

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0609 seconds using 2.69MB