Solved

Why does this search query not work?

Posted on 2013-01-26
3
259 Views
Last Modified: 2013-01-30
Here's my query:

$contestant_query = "select * from news where main_body LIKE '%$_POST[search]%' order by headline";

When I introduce a work that I know for certain is contained in one of the articles, I get nothing, when there should be a list of articles.

What am I missing?
0
Comment
Question by:brucegust
[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
3 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 166 total points
ID: 38823113
Does the query work if you put the actual text in for the variable?  For what it's worth, I always assign $_POST and $_GET variables to 'normal' variables before I try to use them.  Don't know why but it just always seems to work our better.  Like this:

$search = $_POST[search];

Then...

$contestant_query = "select * from news where main_body LIKE '%$search%' order by headline";

Or...

$contestant_query = "select * from news where main_body LIKE '%".$search."%' order by headline";

And when I am having trouble with a query, I echo it to the screen to see if I am getting what I thought I was getting.
0
 
LVL 11

Assisted Solution

by:mcnute
mcnute earned 167 total points
ID: 38823979
backticks are useful also, they protect your query syntax:

$contestant_query = "select * from `news` where `main_body` LIKE '%$_POST[search]%' order by `headline`";

Open in new window

0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 167 total points
ID: 38824413
First and foremost take the essential step to add error_reporting(E_ALL) to the top of all of your PHP scripts and correct any notices that appear.  This will help you avoid using undefined variables or undefined indexes.  It will save you from a debugging mystery!

Next, print out the contents of $_POST like this, and be sure that it contains the information you actually want to search for.

var_dump($_POST);

Next, change your query to use the correct escape sequence.  The code snippet will probably be correct if you're using procedural MySQL. Note the use of the quotes around the array index?  You want to do that; you do not want to rely on PHP making changes to your script silently.  It's always better to write the code correctly.

Check the results resource from the query to see that the query actually worked.  Print any associated error messages.

If the query succeeded, print the number of rows.

If you did not find what you were looking for, use phpMyAdmin to inspect the data base tables.
$safe_search = mysql_real_escape_string($_POST['search']);
$contestant_query = "SELECT * FROM news WHERE main_body LIKE '%$safe_search%' ORDER BY headline";
$res = mysql_query($contestant_query) or die("FAIL: $contestant_query BECAUSE: " . mysql_error());
$num = mysql_num_rows($res);
var_dump($num);

Open in new window

These are things you should be doing with every query, every time.  It's tempting to take shortcuts, and PHP is notorious for permitting sloppy programming, but in the interest of getting accurate results fastest, all of these basic steps play together in ways that set you on the right course.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

749 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