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