Solved

Why does this search query not work?

Posted on 2013-01-26
3
258 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 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 109

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

792 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