How to retrieve MySQL database data using a php search bar - php

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>";
}
}
?>

Related

Search last 4digits and match the exact or nearest result in the database using php

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

Search engine with multiple keywords

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

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

Mysqli multi_query Search of 2 Tables Not showing Results

I'm up to my neck trying to figure out why my query isn't working. This is what my search.php page results in. I am able to _GET the search term perfectly but can't display the results.
Not sure if the issue is the fetch_array_assoc or what! Here's my code. Any help with this would be appreciated. Not 100% sure if my syntax is correct.
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$button = $_GET ['submit'];
$search = $_GET ['query'];
if (strlen($search) <= 1) {
echo "Search term too short";
}
else {
echo "You searched for <b>$search</b> <hr size='1'></br>";
$con = new mysqli("localhost", "user", "pass", "db");
// Check connection
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) {
$query = "Keyword_ID LIKE '%$search_each%' or Keyword_Name LIKE '%$search_each%' ";
}
else {
$query .= "OR Keyword_ID LIKE '%$search_each%' or Keyword_Name LIKE '%$search_each%' ";
}
}
$construct = mysqli_query($con, "SELECT * FROM profileTable WHERE $query");
$construct = mysqli_query($con, "SELECT * FROM addKeywordTable (Keyword_Name) WHERE $query");
$constructs = mysqli_multi_query($construct);
if (mysqli_multi_query($construct)) {
$numrows = mysqli_num_rows($query);
if ($numrows > 0) {
while ($row = mysqli_fetch_assoc($constructs)) {
$key = $row['Keyword_Name'];
$keyID = $row['keyID'];
$fname = $row['FirName'];
$lname = $row['LaName'];
$mname = $row['MName'];
$suffix = $row['Suffix'];
$title = $row['Title'];
$dept = $row['Dept'];
$phone1 = $row['PH1'];
$phone2 = $row['PH2'];
$email = $row['Email'];
$photo = $row['Photo'];
$bio = $row['BioLK'];
$tags = $row['Tags'];
echo '<h2>$fname $lname</h2>';
echo $key;
echo $title;
echo $dept;
}
}
else {
echo "Results found: \"<b>$x</b>\"";
}
}
}
mysqli_close();
?>
I am trying to search two different tables. addKeywordTable and profileTable. Profile table has all of the profile info for a user. The addKeywordTable stores the keywords/tag names 'Keyword_Name'.
I attempted to create a mysqli_multi_query but its not working at all.
I assuming:
$con is set by
$con = mysqli_connect("host", "user", "password", "db");
mysqli_multi_query : you must all sql commands, except the last, terminate with ;
and concenat $construct with .= . Otherwise you overwrite your $construct.
$construct = "SELECT * FROM profileTable WHERE $query ;");
$construct .= "SELECT * FROM addKeywordTable (Keyword_Name) WHERE $query");
don't set $construct with
$construct = mysqli_query($con, "SELECT * FROM profileTable WHERE $query");
your $construct will only become TRUE or FALSE .
with a variable wich contents TRUE or FALSEyou can not call
$constructs = mysqli_multi_query($con,TRUE);
And you call it wrong
$constructs = mysqli_multi_query($construct);
correct
$constructs = mysqli_multi_query($con,$construct);
You call mysqli_multi_query($construct) twice
$constructs = mysqli_multi_query($construct);
if (mysqli_multi_query($construct)) { ...
the first call is not necessary.
call it only with
if (mysqli_multi_query($con,$construct)) { ...
complete wrong is
if (mysqli_multi_query($construct)) {
$numrows = mysqli_num_rows($query);
if ($numrows > 0) {
while ($row = mysqli_fetch_assoc($constructs)) {
$query is at the moment a simple 'string'
$query = "Keyword_ID LIKE '%$search_each%' or Keyword_Name LIKE '%$search_each%' ";
Also wrong
while ($row = mysqli_fetch_assoc($constructs)) {
To retrieve the resultset from the first query you can use mysqli_use_result() or mysqli_store_result(). All subsequent query results can be processed using mysqli_more_results() and mysqli_next_result().
Call it like this instead
if (mysqli_multi_query($con,$construct)) {
if ($result = mysqli_store_result($con)) {
while ($row = mysqli_fetch_row($result)) {
printf("%s\n", $row[0]);
}
mysqli_free_result($result);
Set $x before you do $x++ .
$x = 0;
You can't be sure that $x is always automatically set to 0 .

Passing on unique session variables to the resumerviewer.php page

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

Categories