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>\"";
Related
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.
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();
?>
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);
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);
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();
?>