Page 1 of 2

results not showing

Posted: Fri Feb 11, 2011 11:33 am
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??

Re: results not showing

Posted: Fri Feb 11, 2011 1:54 pm
by John Cartwright
Add an

Code: Select all

echo "SELECT * FROM publications WHERE $construct";
exit();
before your query and post the results.

Re: results not showing

Posted: Sat Feb 12, 2011 8:46 pm
by califdon
I'd suggest you insert a space before the OR when you start building your WHERE clause.

Re: results not showing

Posted: Sat Feb 12, 2011 9:03 pm
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

Re: results not showing

Posted: Sat Feb 12, 2011 9:56 pm
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%'

Re: results not showing

Posted: Sun Feb 13, 2011 11:27 am
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.

Re: results not showing

Posted: Mon Feb 14, 2011 10:32 am
by peachiness
Thank you all for the suggestions..but I am still getting the same results :(

Re: results not showing

Posted: Mon Feb 14, 2011 11:06 am
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

Re: results not showing

Posted: Mon Feb 14, 2011 11:13 am
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...

Re: results not showing

Posted: Mon Feb 14, 2011 11:41 am
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/>";
}
?>


Re: results not showing

Posted: Mon Feb 14, 2011 12:03 pm
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.

Re: results not showing

Posted: Mon Feb 14, 2011 12:50 pm
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/>";
}
?>


Re: results not showing

Posted: Mon Feb 14, 2011 12:54 pm
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

Re: results not showing

Posted: Mon Feb 14, 2011 12:55 pm
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

Re: results not showing

Posted: Mon Feb 14, 2011 1:01 pm
by litebearer
does you config file have ANY security checks?