Basic search engine not working - php

Hello i managed to make a simple search engine its basic so i started working on that..
But i get Access Forbidden and dont know why can someone help me?
with a form i give the $find the word i search and it should search for it..
thx for your time!`
<?php
if ($searching =="yes")
{
echo "Results";
if ($find == "")
{
echo "You forgot to enter a search term";
exit;
}
mysql_connect("localhost","Anton","zouzou13") or die(mysql_error()); mysql_select_db("Ptyxiakh") or die(mysql_error());
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
$data =mysql_query("SELECT * FROM documents WHERE keywords LIKE'%$find%'");
while($result = mysql_fetch_array( $data ))
{
echo $result['document_name'];
echo " ";
echo $result['first_paragraph'];
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>"; }
echo "<b>Searched For:</b> " .$find; } ?>
ok i made it simpler to see if it works like this:
<?php
error_reporting(E_ALL);
mysql_connect("localhost","Anton","zouzou13") or die(mysql_error());
mysql_select_db("Ptyxiakh") or die(mysql_error());
//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM documents WHERE keywords LIKE 'helmets'");
while($result = mysql_fetch_array( $data ))
{
echo $result['document_name'];
echo " ";
echo $result['first_paragraph'];
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
?>
But i get the "Sorry, but we can not find an entry to match your query<br><br>"; so i cant connect to database?its just wont work :(
With PDO it should be like this??:
<?php
//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo "<h2>Results</h2><p>";
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
$pdo = new PDO('mysql:host=localhost;dbname=ptyxiakh', 'Anton', 'zouzou13');
// Otherwise we connect to our Database
//mysql_connect("localhost","Anton","zouzou13") or die(mysql_error());
//mysql_select_db("Ptyxiakh") or die(mysql_error());
// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
//Now we search for our search term, in the field the user specified
//$data = mysql_query("SELECT * FROM documents WHERE keywords LIKE'%$find%'");
$data= $pdo->query("SELECT * FROM documents WHERE keywords LIKE'%$find%'");
//And we display the results
while($row = $statement->fetch(PDO::FETCH_ASSOC))
{
echo htmlentities($row['document_name']);
echo " ";
echo htmlentities($row['first_paragraph']);
echo "<br>";
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>

i made it work its pretty basic and i guess it has deprecated thingys but il upload for ppl that search for things...
just make a simple form and then make a php file
<?php
error_reporting(E_ALL);
//This is only displayed if they have submitted the form
echo "<h2>Results</h2><p>";
$find =$_POST["find"];
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
// Otherwise we connect to our Database
mysql_connect("localhost","Anton","zouzou13") or die(mysql_error());
mysql_select_db("Ptyxiakh") or die(mysql_error());
// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM documents WHERE keywords LIKE'%$find%'");
//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo '<p> <strong>',$result['document_name'], '</strong> <br> ', $result['first_paragraph'],'... <br> </p>';
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
?>
This worked for me :) thx!

Related

Fulltext Search engine, multiple columns, boolean mode

I am making a search engine for an android app that does fulltext search and match against multiple columns against '+word1 +word2' in boolean mode.
However, I can't get any search result.
E.g. search field type- "open sea"
then, Sql will search Match...Against ('+open +sea' IN BOOLEAN MODE)
and display list of selectable results, on which each result clicked, will provide details of the particular result on a new page.
Sorry, I am a newbie in android app development.
Here is my php code for search.php
<?php
# $db = new mysqli('localhost','username','password','db');
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database.
Please try again later.';
exit;
}
if(!empty($_POST)){
$term = $_POST['query'];
$words = explode(" ", trim($term));
$termArray = array();
foreach($words as $word){
if(!empty($word)){
$termArray[] = "+$word";
}
}
$searchquery = implode(" ", $termArray);
if (!$term) {
echo 'You have not entered any search details. Please go back and try again.';
exit;
}
//initial query
$query = "SELECT *
FROM servicetable
WHERE MATCH(title,cat,brand,company)
AGAINST ('".$searchquery."' IN BOOLEAN MODE)
ORDER BY title ASC";
$result = $db->$query;
$num_results = $result->num_rows;
//show user what user searched.
echo $searchquery;
echo "<p>Results found: ".$num_results."</p>";
//counts results.
if ($num_results == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
for ($i=0; $i <$num_results; $i++) {
$row = $result->fetch_assoc();
echo "<p><strong>".($i+1).". Outlet Name: ";
echo stripslashes($row['title']);
echo "</strong><br />Category: ";
echo stripslashes($row['cat']);
echo "<br />Opening Hours: ";
echo stripslashes($row['ophours']);
echo "<br />Brand: ";
echo stripslashes($row['brand']);
echo "</strong><br />Company: ";
echo stripslashes($row['company']);
echo "</p>";
}
$result->free();
$db->close();
} else {
?>
<h1>Search</h1>
<form name="form1" action="search.php" method="post">
Enter Search:<br />
<input type="text" name="query" id="query" placeholder="Search a service"/>
<br/>
<input type="submit" value="Search Now" name="completedsearch" />
</form>
<?php
}
?>
Hi I have found my error:
this line correction-->
$result = $db->query($query);

How to display a certain details from database

I have this code for search in mysql and It is working but when I run and entered a account number it display all accounts in my database
I just want to display details for the account number that I inquire on.
what should I change or add? I don't know
<?php
echo "<h2>Search Results:</h2><p>";
//If they did not enter a search term we give them an error
if ($find == "Account_Number")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}
// Otherwise we connect to our Database
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
// We perform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT Account_Number, Name, Balance FROM memaccounts WHERE ID
LIKE'%$find%'");
//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['Account_Number'];
echo " ";
echo $result['Name'];
echo "<br>";
echo $result['Balance'];
echo "<br>";
echo "<br>";
}
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query...<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
//}
?>
$data = mysql_query("SELECT Account_Number, Name, Balance FROM
memaccounts WHERE Account_Number = '" . trim($find) . "' ");
1.where with Ac no.
Suggestion : Use mysqli_*

creating a page that displays ID info on a template

Updated with suggestion by others but still seem to be stuck.
I'm using this php code here to display info from my database using the ID. I created a link on my main page that looks like this.
<h1><?php echo $row_getDisplay['title']; ?></a></h1>
I have so when they click on the title of the article that it takes them to my php fiel which I named fetch.php and the code below is what is in there. I have built this around someone else's work. For some reason I can't get passed the first "else" statement. so I keep getting "you must select a valid location" I'm fairly new to php so I don't really understand why the code is failing.
<?php require_once('Connections/XXXXXX.php'); ?>
<?php
if (isset($_GET['id']) == false) // check if id has been set
{
echo "You must select a location"; // if not, display this error
exit;
} else {
$id = (int) $_GET['id'];
if (is_numeric($id) == false)
**{
echo "You must select a valid location.";
} else {**
mysql_select_db($database_XXXXXX, $XXXXXX);
$query = MYSQL_QUERY("SELECT * FROM news WHERE post_id ");
if (MYSQL_NUM_ROWS($query) == "1")
{
$fetch = MYSQL_FETCH_ARRAY($query); // set $fetch to have the values from the table
echo "Title: " . $fetch['title'] . "<BR>"; // output the info
echo "Blog: " . $fetch['blog_entry'] . "<BR>"; // etc...
echo "Author: " . $fetch['author'] . "<BR>"; // etc...
} else {
echo "No match in database found."; // if no match is found, display this error
}
}
}
Any help is appreciated. If you are able to find a better solution for me that would be great.
You shouldnt use $HTTP_GET_VARS its deprecated and unless its turned on it wont be populated. use $_GET instead.
if (isset($_GET['id']) == false)
Use $_GET for your if statement:
if (isset($_GET['id']) == false)
Also, you need to convert your $_GET value to an integer, because it is currently a string.
Right after that if statement above, in the else, put this:
$id = (int) $_GET['id'];
That way your is_numeric() will work properly.
Try this;
<?php
require_once('Connections/XXXXXX.php');
if (isset($_GET['id'])) // check if id has been set
{
$id = $_GET['id'];
if (is_numeric($id) == false)
{
echo "You must select a valid location.";
} else {
mysql_select_db($database_XXXXXX, $XXXXXX);
$query = MYSQL_QUERY("SELECT * FROM news WHERE locationid = 'news.post_id' ");
if (MYSQL_NUM_ROWS($query) == "1")
{
$fetch = MYSQL_FETCH_ARRAY($query); // set $fetch to have the values from the table
echo "Title: " . $fetch['title'] . "<BR>"; // output the info
echo "Blog: " . $fetch['blog_entry'] . "<BR>"; // etc...
echo "Author: " . $fetch['author'] . "<BR>"; // etc...
} else {
echo "No match in database found."; // if no match is found, display this error
}
}
}
else{
echo "You must select a location"; // if not, display this error
exit;
}
?>
Also, I need a clarification about news.post_id, from where are you grabbing this?

PHP code not working in IE8

I've got a baffling IE8 PHP problem. The code below works fine in chrome and FF but not IE8. The form is submitting, errors are displayed if no search term or check-boxes selected, and if both db searches fail the No Actors and No Movies found echo's are both displayed. But if either one of the searches is successful nothing is displayed, not even the Actors/Movies found echo which has me stumped.
Here's the code:
<?php
if($_POST[submitbutton]){
$search = trim(mysql_real_escape_string($_POST[search]));
if(!$search){
echo "Please enter a search term!";
}else if(!$_POST['checkbox']){
echo "Please select at least one database to search!";
}else{
//search names
if(in_array("actors", $_POST['checkbox'])){
$query = mysql_query("SELECT name_id, realname, mainalias FROM names WHERE realname LIKE '%$search%' OR mainalias LIKE '%$search%'");
if(mysql_num_rows($query)==0){
echo "<h2>No Actors by that name found!</h2>", "<p>";
}else{
echo "<h2>Actors Found:</h2>";
while ($record = mysql_fetch_assoc($query)){
$realname = $record['realname'];
$name_id = $record['name_id'];
echo "<a href='index.php?page=name&id=$name_id'>", $realname, "</a><hr>";
}
}
}
//search titles
if(in_array("movies", $_POST['checkbox'])){
$query = mysql_query("SELECT title_id, title FROM titles WHERE title LIKE '%$search%'");
if(mysql_num_rows($query)==0){
echo "<h2>No Movies by that name found!</h2>", "<p>";
}else{
echo "<h2>Movies Found:</h2>";
while ($record = mysql_fetch_assoc($query)){
$title = $record['title'];
$title_id = $record['title_id'];
echo "<a href='index.php?page=title&id=$title_id'>", $title, "</a><br>";
echo "<hr>";
}
}
}
}
} //end post submitbutton
?>
Should
$search = trim(mysql_real_escape_string($_POST[search]));
be
$search = trim(mysql_real_escape_string($_POST['search']));
I don't know if that has anything to do with your problem, but it jumper out at me.
<?php
if(isset($_POST['submitbutton'], $_POST['search'])){ //use isset for check exists vars
$search = trim(mysql_real_escape_string($_POST['search'])); // ['POST vars']
if(!$search){
echo "Please enter a search term!";
}else if(!isset($_POST['checkbox'])){
echo "Please select at least one database to search!";
}else{
//search names
if(in_array("actors", $_POST['checkbox'])){
$query = mysql_query("SELECT name_id, realname, mainalias FROM names WHERE realname LIKE '%$search%' OR mainalias LIKE '%$search%'");
if(mysql_num_rows($query)==0){
echo "<h2>No Actors by that name found!</h2>", "<p>";
}else{
echo "<h2>Actors Found:</h2>";
while ($record = mysql_fetch_assoc($query)){
$realname = $record['realname'];
$name_id = $record['name_id'];
echo "<a href='index.php?page=name&id=$name_id'>", $realname, "</a><hr>";
}
}
}
//search titles
if(in_array("movies", $_POST['checkbox'])){
$query = mysql_query("SELECT title_id, title FROM titles WHERE title LIKE '%$search%'");
if(mysql_num_rows($query)==0){
echo "<h2>No Movies by that name found!</h2>", "<p>";
}else{
echo "<h2>Movies Found:</h2>";
while ($record = mysql_fetch_assoc($query)){
$title = $record['title'];
$title_id = $record['title_id'];
echo "<a href='index.php?page=title&id=$title_id'>", $title, "</a><br>";
echo "<hr>";
}
}
}
}
} //end post submitbutton
?>

this program strip would output a searched directory but if not found must output a message

this program must output a directory that you searched for how ever if its not found a message must appear that the org_name is not found,i don't know how to do that, i keep trying on some if-else but it just won't output it.
<?php
$con=mysql_connect("localhost","root","");
if(!$con) {
die('could not connect:'.mysql_error());
}
mysql_select_db("final?orgdocs",$con);
$org_name = $_POST["org_name"];
$position = $_POST["position"];
$result = mysql_query("SELECT * FROM directory WHERE org_name = '$org_name' OR position = '$position' ORDER BY org_name");
echo '<TABLE BORDER = "1">';
$result1 = $result;
echo '<TR>'.'<TD>'.'Name'.'</TD>'.'<TD>'.'Organization Name'.'</TD>'.'<TD>'.'Position'.'</TD>'.'<TD>'.'Cell Number'.'</TD>'.'<TD>'.'Email-Add'.'</TD>';
echo '</TR>';
while ( $row = mysql_fetch_array($result1) ){
echo '<TR>'.'<TD>'.$row['name'].'</TD>'.'<TD>'.$row['org_name'].'</TD>';
echo '<TD>'.$row['position'].'</TD>'.'<TD>'.$row['cell_num'].'</TD>'.'<TD>'.$row['email_add'].'</TD>';
echo '</TR>';
}
echo '</TABLE>';
?>
What you want is mysql_num_rows to check your query for how many result rows it has.
http://de2.php.net/manual/en/function.mysql-num-rows.php
if (mysql_num_rows($result)>0) {
// your thing above with mysql_fetch_array($result1) etc
} else {
echo 'nor found';
}

Categories