Solved

Select rows that contain a specific word (mysql - php)

Posted on 2009-07-10
5
271 Views
Last Modified: 2012-05-07
Hi E's, snippet code contain the code I use to find rows that = $ppp. This code just word when $ppp is exact match of assoc_simultaneo contain.
If the contain of assoc_simultaneo was "one two five twenty" and if $ppp was = "five", the row is not selected. The row was selected if $ppp was = "one two five twenty".
What I want is if $ppp contain one of the words of assoc_simultaneo, the row will be selected. I try to change '$ppp' to '%$ppp%', but don't word.

What changes I have to in my code?

Regards, JC

$assoc_result = mysql_query("SELECT * FROM keywords where assoc_simultaneo = '$ppp'", $db);
$assoc_rows = mysql_num_rows($assoc_result);
$assoc = mysql_fetch_object($assoc_result);

Open in new window

0
Comment
Question by:Pedro Chagas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 8

Accepted Solution

by:
stefanx earned 125 total points
ID: 24827879
$assoc_result = mysql_query("SELECT * FROM keywords where assoc_simultaneo LIKE '%$ppp%'", $db)
0
 
LVL 28

Assisted Solution

by:gamebits
gamebits earned 125 total points
ID: 24827884
mysql_query("SELECT * FROM keywords where assoc_simultaneo LIKE '%$ppp%'", $db);
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 24828028
In case nobody's said this before, it's good programming practice to always sanitize any variables that are used in queries. For example, if $ppp is provided by a user coming to a web page and typing in something to search for, then a user could search for:

%';DROP TABLE keywords;SELECT * FROM whatever WHERE blah LIKE '%

Then, when the query runs, it would execute this query:

$assoc_result = mysql_query("SELECT * FROM keywords where assoc_simultaneo LIKE '%%';DROP TABLE keywords;SELECT * FROM whatever WHERE blah LIKE '%%'", $db);

MySQL isn't smart enough to catch that type of stuff, so it would just run three queries:
SELECT * FROM keywords where assoc_simultaneo LIKE '%%';
DROP TABLE keywords;
SELECT * FROM whatever WHERE blah LIKE '%%';

Presto, the user has just deleted your entire keywords table. It's called SQL injection and there are a lot of articles out on the web on how to prevent this type of thing.

This is just for educational purposes - stefanx and gamebits gave you good answers.
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 24828037
I guess you awarded the points while I was writing that up, so ignore the last line. It made more sense when you hadn't awarded the points yet. :)
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 24828254
Thanks @gr8gonzo.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question