I have a search engine that is working , but only when I search 1 word. Whenever I search multiple keywords I only get 1 result.
Example : In my database I have tags like 'test' and 'hello' ;
Whenever I enter "test hello" and click "Search" it displays :
1 result hello (this being the title of the post with the tag = hello).
My code (search.php - the page where I get the search results):
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
if(!$button) {
echo "you didn't submit a keyword";
} else {
if(strlen($search)<=1) {
echo "Search term too short";
} else {
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","root","root");
mysql_select_db("myschool");
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each) {
$x = NULL;
$construct = NULL;
$x++;
if($x==1) {
$construct .="tag LIKE '%$search_each%'";
} else {
$construct .="OR tag LIKE '%$search_each%'";
}
$construct ="SELECT * FROM posts WHERE $construct";
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);
if ($foundnum==0) {
echo "Sorry, there are no matching result for <b>$search</b>.";
} else {
echo "$foundnum results found !<p>";
while($runrows = mysql_fetch_assoc($run)) {
$title = $runrows ['title'];
$tag = $runrows ['tag'];
echo "<a href='#'><b>$title</b></a><br>";
}
}
}
}
}
?>
Problem is probably around the $x=++ part , because I believe the engine is not displaying or even searching through all the rows in the database , and not displaying when the num row count > 1 .
Thanks in advance guys.
EDIT :
I now get multiple results with the code above BUT I get them in this form :
You searched for hello test postare
1 results found !
HELLO
1 results found !
Test
1 results found !
postare noua
How can I make it add the results in 1 place , and not say it everytime it finds a new result for a different keyword ?
You need to start $x variable before foreach statement, and dont set it as null if you want to use it as an integer.
The $construct variable has the same error, you must be having the same response for three times, thats because you have to close the foreach statement before calling mysql select.
$x = 1;
$construct = NULL;
foreach($search_exploded as $search_each)
{
if($x==1) {
$construct .="tag LIKE '%$search_each%'";
} else {
$construct .="OR tag LIKE '%$search_each%'";
}
$x++;
}
$select ="SELECT * FROM posts WHERE $construct";
...
Last Edit
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
if(!$button) {
echo "you didn't submit a keyword";
} else {
if(strlen($search)<=1) {
echo "Search term too short";
} else {
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","root","root");
mysql_select_db("myschool");
$search_exploded = explode (" ", $search);
$x = 1;
$construct = '';
foreach($search_exploded as $search_each) {
if($x==1) {
$construct .="tag LIKE '%$search_each%'";
} else {
$construct .="OR tag LIKE '%$search_each%'";
}
$x++;
}
$select ="SELECT * FROM posts WHERE $construct";
$run = mysql_query($select);
$foundnum = mysql_num_rows($run);
if ($foundnum==0) {
echo "Sorry, there are no matching result for <b>$search</b>.";
} else {
echo "$foundnum results found !<p>";
while($runrows = mysql_fetch_assoc($run)) {
$title = $runrows ['title'];
$tag = $runrows ['tag'];
echo "<a href='#'><b>$title</b></a><br>";
}
}
}
}
?>
You have a couple issues on your code. You missed a " on this line:
echo "Sorry, there are no matching result for <b>$search</b>";
And the last else does not have an if
Related
I tried to make a search engine but i got a mysql/PHP error:
Fatal error: Uncaught Error: Class 'mysql_connect' not found in /storage/ssd1/238/3211238/public_html/search.php:12 Stack trace: #0 {main} thrown in /storage/ssd1/238/3211238/public_html/search.php on line 12
My Code:
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
if (!$button)
echo "you didn't submit a keyword";
else {
if (strlen($search) <= 1)
echo "Search term too short";
else {
echo "You searched for <b> $search </b> <hr size='1' > </ br > ";
mysql_connect("localhost", "id3211238_base", "Rijk1234");
mysqli_select_db("id3211238_data", "id3211238_base");
$search_exploded = explode(" ", $search);
$x = 0;
foreach ($search_exploded as $search_each) {
$x++;
$construct = "";
if ($x == 1)
$construct .= "keywords LIKE '%$search_each%'";
else
$construct .= "AND keywords LIKE '%$search_each%'";
}
$construct = " SELECT * FROM SEARCH_ENGINE WHERE $construct ";
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);
if ($foundnum == 0)
echo "Sorry, there are no matching result for <b> $search </b>. </br> </br> 1. Try more general words. for example: If you want to search 'how to create a website' then use general keyword like 'create' 'website' </br> 2. Try different words with similar meaning </br> 3. Please check your spelling";
else {
echo "$foundnum results found !<p>";
while ($runrows = mysql_fetch_assoc($run)) {
$title = $runrows ['title'];
$desc = $runrows ['description'];
$url = $runrows ['url'];
echo "<a href='$url'> <b> $title </b> </a> <br> $desc <br> <a href='$url'> $url </a> <p>";
}
}
}
}
?>
try the following:
I'm sure there are still going to be bugs but it should get you a lot closer than before.
read the documentation at http://php.net/manual/en/book.mysqli.php
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_password = 'password';
$db_name = 'name';
$button = $_GET [ 'submit' ];
$search = $_GET [ 'search' ];
if( !$button )
die("you didn't submit a keyword");
}
if( strlen( $search ) <= 1 ){
die("Search term too short");
}
echo "You searched for <b> $search </b> <hr size='1' > </ br > ";
// connect
$db_conn = new mysqli($db_host, $db_user, $db_password, $db_name);
// if errors connecting
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
$button = real_escape_string($button) ;
$search = real_escape_string($search) ;
$search_exploded = explode ( " ", $search );
$x = 0;
foreach( $search_exploded as $search_each ) {
$x++;
$construct = "";
if( $x == 1 ){
$construct .="keywords LIKE '%$search_each%'";
}else{
$construct .="AND keywords LIKE '%$search_each%'";
}
}
$result = $db_conn->query($construct)
if ($result->num_rows == 0){
echo "Sorry, there are no matching result for <b> $search </b>. </br> </br> 1. Try more general words. for example: If you want to search 'how to create a website' then use general keyword like 'create' 'website' </br> 2. Try different words with similar meaning </br> 3. Please check your spelling";
}else {
echo $result->num_rows." results found !<p>";
while( $row = result->fetch_assoc()) {
$title = $row ['title'];
$desc = $row ['description'];
$url = $row ['url'];
echo "<a href='$url'> <b> $title </b> </a> <br> $desc <br> <a href='$url'> $url </a> <p>";
}
}
$result->close();
$mysqli->close();
When checking for errors, there is no need to put the rest of the code in an else statement, for it creates ugly code. instead use "die('message');"
By the way that was a PHP error.
I'm working in a project where i need to search for the last 4digit of a mobile number and display the exact match or if not suggest the nearest number to it.
Example:
if user will search for 8773302 it match the 4digit from the database, it will display 8173302 (from database)
else if not found, it will display other suggestions like:
8173301, 8173303, 8173304, 8173305 and so forth.
CODE:
$existNo = "8673302";
$search = substr($existNo, -4);
$search_exploded = explode (" ", $search);
echo $plan;
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="mobileno LIKE '%$search_each%'";
else
$construct .="AND mobileno LIKE '%$search_each%'";
}
$constructs ="SELECT * FROM mobilenum WHERE $construct OR status = '0'";
$run = mysql_query($constructs);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
{
header ("LOCATION:suggestedNo.php");
}
else
{
$getquery = mysql_query("SELECT * FROM mobilenum WHERE $construct") or die (mysql_error());
//Table Results
while($runrows = mysql_fetch_assoc($getquery))
{
$verifyMobileNo= $runrows ['mobileno'];
$verifyMobileStat= $runrows ['status'];
}
$matchMobileNumber = $verifyMobileNo;
if ($verifyMobileStat == "0"){
header ("LOCATION:matchNo.php");
}
else {
header ("LOCATION:suggestedNo.php");
}
}
Hope somebody can help thanks a lot in advance
I have created a search bar using PHP and I want to retrieve the data I have in my MySQL database.
My code is as follows:
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
if(!$button)
echo "You searched for '<b>$search</b>' <hr size='1'</br>";
$con=mysqli_connect("localhost", "root", "root", "PM_DB");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="keywords LIKE '%$search_each%'";
else
$construct .="AND keywords LIKE '%$search_each%'";
}
$construct ="SELECT * FROM Leads WHERE $construct";
$run = mysqli_query($construct);
$foundnum = mysqli_num_rows($run);
if ($foundnum==0)
echo "There are no results for <b>'$search'</b>. Please check your spelling.";
else
{
echo "$foundnum results found !<p>";
while($runrows = mysqli_fetch_assoc($run))
{
$Company = $runrows['Clients'];
echo "<a href='$Company'><b>Company</b></a><br>";
}
}
?>
Every time I click search it only returns the error message. What am I missing out? Any suggestions will be highly appreciated! Thanks - Tijger.
Try this code
$search_exploded = explode(" ", $search);
$construct = "SELECT * FROM Leads WHERE keywords LIKE ";
$construct .= "'%".implode("%' OR '%", $search_exploded)."%'";
$run = mysqli_query($construct);
Live demo
As I requested the code from OP
I commented the two lines which were causing error for him
He was executing the right query and then the wrong query again ... that is why getting no results returned.
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
if (!$button)
echo "You searched for '<b>$Search</b>' <hr size='1'</br>";
$con = mysqli_connect("localhost", "root", "root", "PM_DB");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$search_exploded = explode(" ", $search);
$construct = "SELECT * FROM Leads WHERE keywords LIKE ";
$construct .= "'%" . implode("%' OR '%", $search_exploded) . "%'";
$run = mysqli_query($construct) or die(mysql_error());
// Why thisssssssssssssssssss??????????? OMG
//$construct = "SELECT * FROM Leads WHERE $construct";
//$run = mysqli_query($construct);
$foundnum = mysqli_num_rows($run) or die(mysql_error());
if ($foundnum == 0)
echo "There are no results for <b>'$search'</b>. Please check your spelling.";
else {
echo "$foundnum results found !<p>";
while ($runrows = mysqli_fetch_assoc($run)) {
$Company = $runrows['Clients'];
echo "<a href='$Company'><b>Company</b></a><br>";
}
}
?>
<?php
//get data
session_start();
$button = $_GET['submit'];
$search = $_GET['search'];
if (!$button)
echo "YOU DIDNT SUBMIT A KEYWORD";
else
{
if (strlen($search)<=2)
echo "SEARCH TERM TOO SHORT";
else
{
echo"You searched for <b>$search</b><hr size='1'>";
//connect to database
include("connect.php");
// a bit of filtering
$search = strtoupper($search);
$search= strip_tags($search);
$search = trim ($search);
$keywords = $getrow['preferedlocation'];
//explode our search term
$search_exploded = explode(" ",$search);
foreach ($search_exploded as $search_each)
{
//construct query
$x++;
if ($x==1)
$construct .= "preferedlocation LIKE '%$search_each%'";
else
$construct .= " OR preferedlocation LIKE '%$search_each%'";
}
//echo out construct
$construct = "SELECT * FROM jobseekers 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($run))
{
$jobseekerid = $runrows['jobseekerid'];
$jobseekeremail = $runrows['jobseekeremail'];
$jobseekerpassword = $runrows['jobseekerpassword'];
$jobseekertitle = $runrows['jobseekertitle'];
$jobseekerfirstname = $runrows['jobseekerfirstname'];
$jobseekerlastname = $runrows['jobseekerlastname'];
$jobseekeraddress = $runrows['jobseekeraddress'];
$jobseekerphoneno = $runrows['jobseekerphoneno'];
$jobseekerdob = $runrows['jobseekerdob'];
$jobseekergender = $runrows['jobseekergender'];
$imagelocation = $runrows['imagelocation'];
$preferedlocation = $runrows['preferedlocation'];
$preferedcategory = $runrows['preferedcategory'];
$_SESSION['jobseekerid']= $jobseekerid;
echo "
<b>$jobseekerid
<b>$jobseekerfirstname $jobseekerlastname </b><br>
$jobseekeremail<br>
$jobseekeraddress<br>
$jobseekerphoneno<br>
<br><a href='resumeviewer.php'> Resume information</a></p>";
}
}
}
}
?>
You should pass only jobseekerid to resumerviewer.php
echo "
<b>$jobseekerid
<b>$jobseekerfirstname $jobseekerlastname </b><br>
$jobseekeremail<br>
$jobseekeraddress<br>
$jobseekerphoneno<br>
<br><a href='resumeviewer.php?id=" . $jobseekerid . "'> Resume information</a></p>";
in resumerviewer.php:
$run = mysql_query('SELECT * FROM `jobseekers` WHERE `jobseekerid`=' . intval($_GET['id']));
if(mysql_num_rows($run))
{
$row = mysql_fetch_assoc($run);
print_r($row);
}
For some reason my search script will only display the first set of results for the first page but when I click on the pagination link to the next page the results that are generated from the search script will not be displayed how can I correct this problem?
Here is my PHP & MySQL pagination code.
$x = '';
$construct = '';
if(isset($_POST['search'])) {
$search = $_POST['search'];
if(strlen($search) <= 2){
echo '';
} else {
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
mysqli_select_db($mysqli, "sitename");
$search_explode = explode(" ", $search);
foreach($search_explode as $search_each) {
$x++;
if($x == 1){
$construct .= " article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
} else {
$construct .= " OR article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
}
}
$construct = "SELECT users.*, users_articles.* FROM users_articles
INNER JOIN users ON users_articles.user_id = users.user_id
WHERE $construct";
$run = mysqli_query($mysqli, $construct);
$search_term = mysqli_num_rows($run);
}
}
// Number of records to show per page:
$display = 10;
// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined.
$pages = mysqli_real_escape_string($mysqli, htmlentities(strip_tags($_GET['p'])));
} else { // Need to determine.
// Count the number of records:
$records = $search_term;
// Calculate the number of pages...
if ($records > $display) { // More than 1 page.
$pages = ceil ($records/$display);
} else {
$pages = 1;
}
} // End of p IF.
// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
$start = mysqli_real_escape_string($mysqli, htmlentities(strip_tags($_GET['s'])));
} else {
$start = 0;
}
// Make the links to other pages, if necessary.
if ($pages > 1) {
// Add some spacing and start a paragraph:
echo '<p>';
// Determine what page the script is on:
$current_page = ($start/$display) + 1;
//add this here... first will always be one
if ($current_page != 1) {
echo 'First';
}
// If it's not the first page, make a Previous button:
if ($current_page != 1) {
echo 'Previous ';
}
//create the links
for ($i = max(1, $current_page - 3); $i <= min($current_page + 3, $pages); $i ++) {
if ($i != $current_page) {
echo '' . $i . ' ';
} else {
echo '<span>' . $i . '</span> ';
}
}
// If it's not the last page, make a Next button:
if ($current_page != $pages) {
echo 'Next';
}
//add this here... Last will always be one
if ($current_page != $pages) {
echo 'Last';
}
echo '</p>'; // Close the paragraph.
} // End of links section.
Here is the part of PHP & MySQL search code.
$x = '';
$construct = '';
if(isset($_POST['search'])) {
$search = $_POST['search'];
if(strlen($search) <= 2){
echo 'Your search term is too short!';
} else {
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
mysqli_select_db($mysqli, "sitename");
$search_explode = explode(" ", $search);
foreach($search_explode as $search_each) {
$x++;
if($x == 1){
$construct .= " article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
} else {
$construct .= " OR article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
}
}
$construct = "SELECT users.*, users_articles.* FROM users_articles
INNER JOIN users ON users_articles.user_id = users.user_id
WHERE $construct";
$construct .= " LIMIT $start, $display";
$run = mysqli_query($mysqli, $construct);
$foundnum = mysqli_num_rows($run);
if ($foundnum == 0) {
echo 'Search term is too short!</p>No results found.';
} else {
echo 'results';
}
}
}
That's a lot of code to look through, but could I hazard a guess that $_POST['search'] isn't set when you click on a pagination link, thus causing your entire second block of code not to be run?
Use Session . Below I show you an example of how to do it.
$number = $_POST['studentid'];
if (empty($number)) {
$number=$_SESSION['number'];
}
else{$number=$number ; )
//take a look
At the top of the page put - <? session_start(); ?>
Put the following just before the script for navigation (Next Page/Previous Page)
$_SESSION['number']=$number;
$_COOKIE['number'] = $number;