results not showing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

peachiness
Forum Commoner
Posts: 41
Joined: Mon Oct 11, 2010 1:33 pm

results not showing

Post by peachiness »

Code: Select all


<?php

//get data

$button = $_GET['submit'];
$search = $_GET['search'];

if (!$button) 
	echo "You didn't submit a keyword.";
else
{
if (strlen($search)<=2)
echo "search term too short.";
else
{
echo "You searched for $search <hr size='1'>";


//connect to database and search

mysql_connect("localhost", "", "");

mysql_select_db ("a2288820_data");



//explode our search term

$search_exploded = explode("", $search);

// doughhhhh

foreach($search_exploded as $search_each)

//construct query

$x++;
if ($x==1)
$construct .="keyword1 LIKE '%$search_each%'";
else
$construct .="OR keyword1 LIKE '%$search_each%'";


//echo out construct
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
$construct = "SELECT * FROM publications WHERE $construct";
$run = mysql_query($construct);

$foundnum = mysql_num_rows($run);

if ($foundnum==0)
echo "No results found.";
else
{
echo "$foundnum results found!<p>";

while ($runrows = mysql_fetch_assoc())
{

//get data

$author = $runrows['author1'];

echo "echo $construct";


}

}


}
}

?>
Why is it always displaying 'result not found' when I conduct a search purposely knowing the searched item is available??
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: results not showing

Post by John Cartwright »

Add an

Code: Select all

echo "SELECT * FROM publications WHERE $construct";
exit();
before your query and post the results.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: results not showing

Post by califdon »

I'd suggest you insert a space before the OR when you start building your WHERE clause.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: results not showing

Post by Jonah Bron »

califdon is right. This is the line he's talking about.

Code: Select all

$construct .= "OR keyword1 LIKE '%$search_each%'";
Needs to be

Code: Select all

$construct .= " OR keyword1 LIKE '%$search_each%'";
//             ^ space here
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: results not showing

Post by John Cartwright »

While I agree that a space should be added, the following query would in fact still be syntactically fine.

Code: Select all

SELECT * FROM publications WHERE keyword1 LIKE '%$search_each%'OR keyword1 LIKE '%$search_each%'
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: results not showing

Post by califdon »

John Cartwright wrote:While I agree that a space should be added, the following query would in fact still be syntactically fine.

Code: Select all

SELECT * FROM publications WHERE keyword1 LIKE '%$search_each%'OR keyword1 LIKE '%$search_each%'
So you're saying that it works if the operator immediately follows a closing quote? I didn't know that, but then of course I try very hard never to do that.
peachiness
Forum Commoner
Posts: 41
Joined: Mon Oct 11, 2010 1:33 pm

Re: results not showing

Post by peachiness »

Thank you all for the suggestions..but I am still getting the same results :(
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: results not showing

Post by litebearer »

Maybe I am blind today but..

Code: Select all

keyword1 LIKE '%$search_each%'OR keyword1 LIKE '%$search_each%'
maybe need keword2 or some other field as the 2nd field - the above statement is simply checking the same field twice identically, Soooooo if the item you KNOW is in the table but in a field other than keyword1, obviously it will return not found
peachiness
Forum Commoner
Posts: 41
Joined: Mon Oct 11, 2010 1:33 pm

Re: results not showing

Post by peachiness »

litebearer wrote:Maybe I am blind today but..

Code: Select all

keyword1 LIKE '%$search_each%'OR keyword1 LIKE '%$search_each%'
maybe need keword2 or some other field as the 2nd field - the above statement is simply checking the same field twice identically, Soooooo if the item you KNOW is in the table but in a field other than keyword1, obviously it will return not found
Thanks, but I have been searching the keywords in 'keyword1' field. I tried adding a different search criteria, but I still get no results...
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: results not showing

Post by litebearer »

Simple test. Copy this to a temp file named search_me.php. The make adjustments per the comments ONLY.

Tell us what happens.

Code: Select all

<?PHP
/* USE YOUR DB CONNECTION HERE */
include('db.php');

/* REPLACE THIS VALUE WITH A KNOWN VALUE */
$search_term = "ea";

/* REPLACE  TABLE NAME AND FIELD NAME WITH YOURS */
$query = "SELECT * FROM addresses WHERE lastname LIKE '%$search_term%'";
$result = mysql_query($query);


while($row = mysql_fetch_array($result)) {
	/* REPLACE FIELD NAMES WITH TWO OF YOURS */
	echo $row['lastname'] . " - " . $row['firstname'] . "<br/>";
}
?>

User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: results not showing

Post by califdon »

litebearer wrote:Maybe I am blind today but..

Code: Select all

keyword1 LIKE '%$search_each%'OR keyword1 LIKE '%$search_each%'
maybe need keword2 or some other field as the 2nd field - the above statement is simply checking the same field twice identically, Soooooo if the item you KNOW is in the table but in a field other than keyword1, obviously it will return not found
You're not blind, but I guess I am. That's funny that something that obvious wasn't spotted immediately.

But this does raise the issue that whenever you have to search more than one field for the same search term, your table schema is not normalized, so you're going to continually have problems, not only searching, but potentially with other issues. Relational databases work properly only when their schema is normalized.
peachiness
Forum Commoner
Posts: 41
Joined: Mon Oct 11, 2010 1:33 pm

Re: results not showing

Post by peachiness »

I got

You do not have permission to view this page.

was the db.php supposed to be replaced by something like 'config.php' with this type of info..?
if($indexCheck == "yes")
{
//datbase
$db_user = "xx";
$db_pass = "xx";
$db_host = 'localhost';
mysql_connect($db_host,$db_user,$db_pass);
@mysql_select_db('a2288820_data') or die( "Unable to select database");

litebearer wrote:Simple test. Copy this to a temp file named search_me.php. The make adjustments per the comments ONLY.

Tell us what happens.

Code: Select all

<?PHP
/* USE YOUR DB CONNECTION HERE */
include('db.php');

/* REPLACE THIS VALUE WITH A KNOWN VALUE */
$search_term = "ea";

/* REPLACE  TABLE NAME AND FIELD NAME WITH YOURS */
$query = "SELECT * FROM addresses WHERE lastname LIKE '%$search_term%'";
$result = mysql_query($query);


while($row = mysql_fetch_array($result)) {
	/* REPLACE FIELD NAMES WITH TWO OF YOURS */
	echo $row['lastname'] . " - " . $row['firstname'] . "<br/>";
}
?>

litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: results not showing

Post by litebearer »

It is supposed to be a COMPLETELY separate file from your other scripts.
The only changes you need to make to it are those commented.
then point your browser to the file

Since only you will see the contents of the file - you can put the actual database connection code in the file rather than doing any include
Last edited by litebearer on Mon Feb 14, 2011 12:56 pm, edited 1 time in total.
peachiness
Forum Commoner
Posts: 41
Joined: Mon Oct 11, 2010 1:33 pm

Re: results not showing

Post by peachiness »

yeah, that's what I meant. It's in my config.php file

The example under that was what's inside the config file. I only changed db.php to config.php

Code: Select all

<?PHP
/* USE YOUR DB CONNECTION HERE */
include('config.php');

/* REPLACE THIS VALUE WITH A KNOWN VALUE */
$search_term = "master thesis";

/* REPLACE  TABLE NAME AND FIELD NAME WITH YOURS */
$query = "SELECT * FROM publications WHERE type LIKE '%$search_term%'";
$result = mysql_query($query);


while($row = mysql_fetch_array($result)) {
        /* REPLACE FIELD NAMES WITH TWO OF YOURS */
        echo $row['author1'] . " - " . $row['author2'] . "<br/>";
}
?>
litebearer wrote:It is supposed to be a COMPLETELY separate file from your other scripts.
The only changes you need to make to it are those commented.
then point your browser to the file
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: results not showing

Post by litebearer »

does you config file have ANY security checks?
Post Reply