Solved

Why does this search query not work?

Posted on 2013-01-26
3
254 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
3 Comments
 
LVL 82

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 108

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now