Search Bar coding for multiple keywords when only 1 matches - php

So I have looked around the internet and actually signed up to this site since i find most my google searches wind me up here. I am almost done making my website and I used a template that I expanded. It has a search bar on it that wasn't functioning. I have got it working mostly. the problem I am having is that when I put in a keyword that MySQL has it works fine, when I put in a lone keyword that I know doesn't exist it give a proper no results response. Now my problem is when you mix the 2 up it errors out the database. Here is my php code that I've put together so far...
<?php
$db = mysqli_connect('host','username','password')
or die('Error connecting to MySQL server.');
$keyword = $_GET['keyword'];
$terms = explode(" ", $keyword);
$query = "SELECT * FROM searchresults WHERE ";
foreach ($terms as $each) {
$i=0;
if ($i == 0)
$query .= "pagebody LIKE '%$each%' ";
else
$query .= " OR pagebody LIKE '%$each%' ";
}
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);
if ($row > 0) {
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$pagebody = $row['pagebody'];
$link = $row['link'];
echo "<h2><a href='$link'>$title</a></h2></br>
$description<br/><br/>";
}
} else
echo "No results found for.\"<b>$keyword</b>\"";
?>
Any help would be much appreciated. I need to figure out how to make it so that if any of the keywords aren't recognized that it doesn't error out.

This won't fix the problems you have escaping your user input, but to answer your question in resolving the search error; your original code is keeping $i as equalling 0 through each iteration of the foreach loop so the query is coming out as:
SELECT * FROM searchresults WHERE pagebody LIKE '%$each%' pagebody LIKE '%$each%' pagebody LIKE '%$each%'
You need to modify the following lines:
$query = "SELECT * FROM searchresults WHERE ";
foreach ($terms as $each) {
$i=0;
if ($i == 0)
$query .= "pagebody LIKE '%$each%' ";
else
$query .= " OR pagebody LIKE '%$each%' ";
}
change that to:
$i=0;
$query = "SELECT * FROM searchresults WHERE";
foreach ($terms as $each) {
if($i == 0){
$query .= " pagebody LIKE '%$each%'";
}else{
$query .= " OR pagebody LIKE '%$each%'";
}
$i++;
}

Related

PHP Search Box not working?

I have created a search box that searches my website. I have tried really hard to fix it but it didn't work. Please could you help? The code is below:
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE "
for each ($terms as $each) {
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
//connect
mysql_connect("localhost", "my username", "my password");
mysql_select_db("my database");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ( $row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h1><a href='$link'>$title</a></h2>";
$description<br /><br />""
}
}
else
echo "No results found for \"<b>$k/b>\" ";
//disconnect
mysql_close();
?>
first of all, correct syntax. Instead of:
$query = "SELECT * FROM search WHERE "
for each ($terms as $each) {
should be
$query = "SELECT * FROM search WHERE ";
foreach ($terms as $each) {
and OUH, what's this???
$description<br /><br />""
maybe youd learn the basics of syntax at first.

results.php shows all old queries

I created a website search bar using php with mysql, my results page shows all old queries. I even tried from other computers and search queries still appear. I do not have duplicate keywords so that should not be an issue. How can I get the results page to dump old queries?
<?php
$input = $_GET['input'];//Note to self $input in the name of the search feild
$terms = explode(" ", $input);
$query = "SELECT * FROM search WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
// connecting to our mysql database
mysql_connect("localhost", "pti_user", "policetech1");
mysql_select_db("pti_db");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h2><a href='$link'>$title</a></h2>
$description<br /><br />";
}
}
else
echo "No results found for \"<b>$input</b>\"";
// disconnect
mysql_close();
?>

php Search engine the keywords are not working

When i would search for the keywords that i have specified in my database it will return everything from my database not just the corresponding links that have the keywords attached to the link. here is my code
<?php
$q = $_GET['q'];
$terms = explode(" ", $q);
$query = "SELECT * FROM search ";
foreach ($terms as $each){
$i=0;
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
//connect
mysql_connect("localhost", "root", "");
mysql_select_db("search");
$query = mysql_query("SELECT * FROM search");
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h3><a href='$link'>$title</a></h3><h4>$link</h4>$description<br /><br />";
}
}
else
echo "<b>No Results Found</b><br><br>Suggestions:<br>
Make sure all words are spelled correctly.<br>
Try different keywords.<br>
Try more general keywords.";
//disconnect
mysql_close();
?>
<?php
$q = $_GET['q'];
$terms = explode(" ", $q);
//connect
mysql_connect("localhost", "root", "");
mysql_select_db("search");
$query = "SELECT * FROM search ";
$i=1;
foreach ($terms as $each){
if ($i == 1) {
$query .= "WHERE ";
$query .= "keywords LIKE '" . mysql_real_escape_string("%" . $each . "%") . "' ";
} else {
$query .= "OR keywords LIKE '" . mysql_real_escape_string("%" . $each . "%") . "' ";
}
$i++;
}
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h3><a href='$link'>$title</a></h3><h4>$link</h4>$description<br /><br />";
}
} else {
echo "<b>No Results Found</b><br><br>Suggestions:<br>
Make sure all words are spelled correctly.<br>
Try different keywords.<br>
Try more general keywords.";
}
//disconnect
mysql_close();
?>
Fixes:
1) Removed second $query that was being defined. It selected all rows.
2) Moved initial $i declaration. It was being set back to 0 each loop.
3) Added WHERE
4) Moved $i++ after the if statement and set initial $i to 1.
5) Added mysql_real_escape_string so that data is escaped properly.
Recommendations:
I highly recommend taking a look at MySQLi (http://us2.php.net/mysqli) or PDO (http://us3.php.net/pdo)
Please let me know if this works or if you need further assistance.
A first sight, i see a couple of errors.
$i=0;
$i++;
if ($i == 1)
$i Will ALWAYS be one are.
you might want to move $i = 0; BEFORE the foreach
$query = mysql_query("SELECT * FROM search");
You build a query, but in the end you're not using it. you probably want to do : $query = mysql_query($query); instead. ( and also for code clarity using a different variable name for the output ? ) .
mysql_query is deprecated. Useless you're in a hurry, check PDO
First, you're missing the WHERE keyword before the conditions. So it should be:
foreach ($terms as $i => $each){
$each = mysql_real_escape_string($each); // Prevent SQL injection
if ($i == 0)
$query .= "WHERE keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
You don't need to increment your own counter variable, you can use the array indexes from $terms.
Second, after all that work to create $query, you're not using it. You wrote:
$query = mysql_query("SELECT * FROM search");
That should be:
$query = mysql_query($query);
BTW, it's generally a bad idea to reuse variables like that, it gets confusing when you use the same variable for different things. I suggest you call the second $query something like $results.
Change this line
$query .= "keywords LIKE '%$each%' ";
By
$query .= " Where keywords LIKE '%$each%' ";
And also cnhange this line
$query = mysql_query("SELECT * FROM search");
By
$query = mysql_query($query);

Make the search engine ignore specific keywords

I'm currently using
$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE ";
foreach ($terms as $each) {
$i++;
if ($i == 1)
$query .= "keywords = '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
// connect
mysql_connect("localhost", "root", "password");
mysql_select_db("search");
$query = mysql_query($query);
$numrows = mysql_num_rows ($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$links = $row['link'];
echo "<h2>$title</h2><p>$description</p>";
}
}
else
echo "No Search Results have been found for \"<b>$k</b>\"";
// disconnect
mysql_close();
?>
this piece of code I learned from a tutorial to begin my search engine. I'll be honest, I don't know any PHP, and understanding what's going on here has been a real struggle.
How do I manipulate this piece of code such that if I search "Swarthmore College" or "Swarthmore", Swarthmore results will show up. I need to make sure that just typing "College" or "swar" or "a" won't trigger the Swarthmore results.
But I need common words like "University", "of", and "Chicago" to trigger together for college searches like "University of Chicago." It should also be triggered by abbreviations like "UChicago."
I'm sorry if this is a lot to ask, but I am completely lost.
Thank you in advance.
You should include an elseif clausule. The code would be:
if ($numrows == 1) {
// You dont need to iterate if you want to show only unique result
$row = mysql_fetch_assoc($query);
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$links = $row['link'];
echo "<h2>$title</h2><p>$description</p>";
}
elseif ($numrows > 1)
echo "More than one Search Results have been found for \"<b>$k</b>\". Please be more specific ";
else
echo "No Search Results have been found for \"<b>$k</b>\"";

PHP Search Script not working

I am trying to run a simple PHP search script that will show look through my mySQL database and present the results on a separate page that I've created. When I run the code, my results don't show up. Can someone please help me?? This is the code that i'm currently using:
<?php
error_reporting(E_A`enter code here`LL);
ini_set('display_errors', 1);
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE ";
$i = 0;
foreach ($terms as $each){
$i++;
if ($i == 1)
{
$query .= "keywords LIKE '%$each%' ";
}
else
{
$query .= "OR keywords LIKE '%$each%' ";
}
}
// connect
mysql_connect("josetogbecom.fatcowmysql.com","username","password");
mysql_select_db("gff_hff6a144eg");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h2><a href='$link'>$title</a></h2>
$description<br /><br />";
}
}
else
{
echo "No Search Results Were Found for \"<b>$k<b>\"";
}
// disconnect
mysql_close();
?>
Thanks in advance!
Check if your $i counter var is set to $i=0 before foreach loop
Bug at this line:
echo "No Search Results Were Found for \"<b>$k<b>/"";
Should be:
echo "No Search Results Were Found for \"<b>$k<b>\"";
Bug at this line:
mysql close();
Should be:
mysql_close();
Bug at this line:
$i++;
You should always initialize your variables, like this:
$i = 0;
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '$each%' ";
else
$query .= "OR keywords LIKE '$each%' ";
}
If the script still doesn't work after correcting these two bugs, add to the beginning of the script these 2 lines and tell us what you see:
error_reporting(E_ALL);
ini_set('display_errors', 1);

Categories