php Search engine the keywords are not working - php

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);

Related

Search Bar coding for multiple keywords when only 1 matches

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++;
}

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();
?>

How to limit the amount of search results on each page like Google?

I am trying to limit the amount of search results on each page like Google does. I am also using a SQL database to organize my keywords and everything. Here is my code
<?php
$q = $_GET['q'];
$terms = explode(" ", $q);
//connect
mysql_connect("localhost", "root", "") or die ("Could Not Connect");
mysql_select_db("search") or die ("Could Not Connect To database");
$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();
?>
After your foreach ($terms as $each){} you need to add an ORDER BY and LIMIT to the query, like so:
$query .= "ORDER BY keywords LIMIT 0, 10";
That will give you the first 10 results. Alter the LIMIT vars as needed. The first number is the offset (zero based, aka first result is 0, sixth result is 5) and the second number is the number of rows to return.
The ORDER BY helps with consistent results. You don't need it, but sometimes results can be weird.
See this question for more info if you need it.

Can I have files automatically be added to my MySQL database?

I have a custom search function on my site, using PHP. I don't like having to go to my database every time I make a page, so can I write something to automatically add a new page to my database? Here's my code:
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
//connect
mysql_connect("*****", "*****", "*****");
mysql_select_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 "<h3><a href='$link'>$title</a></h3>
<hr />";
}
}
else
echo "No Results Found for \"<b>$k</b>\". Try Again.";
//disconnect
mysql_close();
?>

Categories