Solved

Why does this search query not work?

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

Independent Software Vendors: 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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

739 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