Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Why does this search query not work?

Posted on 2013-01-26
3
Medium Priority
?
262 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 84

Accepted Solution

by:
Dave Baldwin earned 664 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 668 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 668 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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 i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
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.

688 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