i am having problem that when i enter only first name eg; rahul it gives search results but when i enter full name eg; rahul kapoor it does not give anything and shows no such users.
i am using javascript for getting search results from search.php here is search.php:
<?php
if($_POST)
{
$q=$_POST['searchword'];
$sql_res=
mysql_query("select * from users where first_name like '%$q%' or last_name like '%$q%' order by id LIMIT 5");
while($row=mysql_fetch_array($sql_res))
{
$fname=$row['fname'];
$lname=$row['lname'];
$img=$row['profile_pic'];
$re_fname='<b>'.$q.'</b>';
$re_lname='<b>'.$q.'</b>';
$final_fname = str_ireplace($q, $re_fname, $fname);
$final_lname = str_ireplace($q, $re_lname, $lname);
?>
<div class="display_box" align="left">
<img src="usedata/profile_pics/
<?php echo $img; ?>" />
<?php echo $final_fname; ?>
<?php echo $final_lname; ?><br/>
</div>
<?php
}
}
else
{}
?>
As #Mark Baker and #BenM pointed out you may try something as
mysql_query("select * from users where first_name like '%$q%' or last_name like '%$q%' or concat(first_name,' ',last_name) = '$q' or concat(first_name,' ',last_name) like '%$q%'order by id LIMIT 5");
Try this:
$q=$_POST['searchword'];
list($q1,$q2) = explode(" ", $q);
$sql_res=
mysql_query("select * from users where (first_name like '%$q1%' OR first_name like '%$q2%') or (last_name like '%$q1%' OR last_name like '%$q2%') order by id LIMIT 5");
Or something similar.
It's because you have to split the first and last name...
Related
code:
<?php
session_start;
include('config.php');
$student_id = $_SESSION['student_id'];
$searchTerm = $_GET['term'];
$query = "SELECT company_name FROM company WHERE company LIKE '%".$searchTerm."%' ORDER BY company_name ASC";
$query .= "SELECT key_skills FROM skill_master WHERE key_skills LIKE '%".$searchTerm."%' ORDER BY key_skills ASC";
$result = mysqli_multi_query($con,$query);
while($row = mysqli_fetch_assoc($result))
{
$data[] = $row['company_name'];
$data[] = $row['key_skills'];
}
echo json_encode($data);
?>
I am using autocomplete suggestion box when I use single query in the above code it work perfectly and show me result but now I want to run two query to get different data in a single autocomplete box but when I run this file it show me "null" output. So, How can I fix and get two different table data in single box ?Please help me.
Thank You
The below code was modified to get results from two queries.
Try this:
<?php
session_start();
include('config.php');
$student_id = $_SESSION['student_id'];
$data = array();
$searchTerm = $_GET['term'];
$query = "SELECT company_name FROM company WHERE company LIKE '%".$searchTerm."%' ORDER BY company_name ASC;";
$query .= "SELECT key_skills FROM skill_master WHERE key_skills LIKE '%".$searchTerm."%' ORDER BY key_skills ASC;";
mysqli_multi_query($con,$query);
$result = mysqli_store_result($con);
while($row = mysqli_fetch_assoc($result))
{
$data[] = $row['company_name'];
}
mysqli_free_result($result);
mysqli_next_result($con);
$result = mysqli_store_result($con);
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row['key_skills'];
}
mysqli_free_result($result);
mysqli_close($con);
echo json_encode($data);
?>
Try this,
$query = "SELECT company_name FROM company WHERE company LIKE '%".$searchTerm."%' ORDER BY company_name ASC
UNION
SELECT key_skills FROM skill_master WHERE key_skills LIKE '%".$searchTerm."%' ORDER BY key_skills ASC";
The following code is a part of my php script i build in order to display results from a mysql database. It is executed fine and I get the desirable results. However, when I run the script with both variables ("$search" and "$daterange") not set, I get a "Notice: Undefined offset" on the line I provide to you between " * ". That Notice is not appeared in any other occasion.
<form action="" method="post">
Query: <input type="text" name="search" style="margin-bottom: 0px;" />
<div class="input-prepend">
<span class="add-on"></span>
<input name="daterange" id="reportrange" type="text" value=""></input>
</div>
<input id="button" type="submit" name="submit" value="Search Database"/>
</form>
<?php
$conn = mysql_connect($server, $user, $pass) or die (mysql_error());
mysql_select_db($database, $conn);
$search = false;
$daterange = false;
$from = false;
$to = false;
if(isset($_POST['search'])) {
$search = $_POST['search'];
}
// ***
if(isset($_POST['daterange'])) {
$daterange = $_POST['daterange'];
list($from, $to) = preg_split('[ to ]', $daterange);
}
// ***
if ($daterange==false) {
$sql = "SELECT calendar.id, DATE(`start`) AS Date, CONCAT(TIME_FORMAT(`start`,'%H:%i'),'-',TIME_FORMAT(`end`,'%H:%i')) AS Time, title, description, 'open', fatherName, birthDate, occupation, address, postcode, cityArea, country, telNum, cellNum FROM patients INNER JOIN calendar ON title=CONCAT(lastName,' ',firstName) WHERE title like '%$search%' OR description like '%$search%' OR lastName like '%$search%' OR firstName like '%$search%' OR fatherName like '%$search%' OR birthDate like '%$search%' OR occupation like '%$search%' OR address like '%$search%' OR postcode like '%$search%' OR cityArea like '%$search%' OR country like '%$search%' OR telNum like '%$search%' OR cellNum like '%$search%' OR email like '%$search%' OR insuranceInstitution like '%$search%' OR insuranceId like '%$search%' ORDER BY Date DESC";
} else {
$sql = "SELECT calendar.id, DATE(`start`) AS 'Date', CONCAT(TIME_FORMAT(`start`,'%H:%i'),'-',TIME_FORMAT(`end`,'%H:%i')) AS Time, title, description, 'open', fatherName, birthDate, occupation, address, postcode, cityArea, country, telNum, cellNum FROM patients INNER JOIN calendar ON title=CONCAT(lastName,' ',firstName) WHERE (calendar.id like '%$search%' OR title like '%$search%' OR description like '%$search%' OR patientId like '%$search%' OR lastName like '%$search%' OR firstName like '%$search%' OR fatherName like '%$search%' OR birthDate like '%$search%' OR occupation like '%$search%' OR address like '%$search%' OR postcode like '%$search%' OR cityArea like '%$search%' OR country like '%$search%' OR telNum like '%$search%' OR cellNum like '%$search%' OR email like '%$search%' OR insuranceInstitution like '%$search%' OR insuranceId like '%$search%') AND (DATE(`start`) BETWEEN DATE_FORMAT(STR_TO_DATE('$from', '%d-%m-%Y'), '%Y-%m-%d') AND DATE_FORMAT(STR_TO_DATE('$to', '%d-%m-%Y'), '%Y-%m-%d'))";
}
$result = mysql_query($sql, $conn) or die(mysql_error());
.
.
//the rest of the code
.
.
?>
Thanks in advance for your help!
Are you sending empty variables?
in that case use
if( !empty($_POST['daterange']) ){
}
empty returns true if the variable is an empty string, false, array(), NULL, “0?, 0, or an unset variable.
Read more: http://techtalk.virendrachandak.com/php-isset-vs-empty-vs-is_null/
Try to replace:
if(isset($_POST['daterange']))
By:
if (array_key_exists('daterange',$_POST))
I am trying to search the field firstname and lastname for a keyword
$q1 = strtolower($_GET["q"]);
$q=str_replace(" ","%",$q1);
$sql = "select DISTINCT users.*, user_id FROM users WHERE $email_filter
firstname LIKE '%$q%' OR lastname LIKE '%$q%' ORDER BY lastname";
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) { echo $results }
this is what I have so far, issue is if you use John Doe as an example once you type John it finds it, doe it finds it, but john doe ... no results
I recommend that you bind the variables. You are exposed to sql injections otherwise.
$stmt = $mysqli->prepare("select * from users where firstname like ? AND lastname like ?");
$stmt->bind_param('ss', $firstname,$lastname);
Something like
SELECT * FROM users where CONCAT(firstname, ' ', lastname) like '%$q%'
Or
SELECT * FROM users where CONCAT_WS(' ', firstname, lastname) like '%$q%'
And if reversing is desirable, try this:
SELECT * FROM users where CONCAT_WS(' ', firstname, lastname) like '%$q%'
or CONCAT_WS(' ', lastname, firstname) like '%$q%'
(that is, if searching for "A B" should return "A B" as well as "B A")
you have to split your query string and search for each terms
$query_terms = explode(" ", $q1);
$conditions = ''
foreach($query_terms as $term){
$conditions = $conditions.' firstname LIKE "%'.$term.'%" OR lastname LIKE "%'.$term.'%"';
}
$sql = "select DISTINCT users.*, user_id FROM users WHERE $email_filter $conditions ORDER BY lastname";
I want to only display 5 results from my database with my PHP search script. How can I do this?
My PHP code is:
<?php
mysql_connect("localhost","username","password");
mysql_select_db("database");
if(!empty($_GET['q'])){
$query= mysql_real_escape_string(trim($_GET['q']));
}
$searchSQL = "SELECT * FROM links WHERE `title` LIKE '%{$query}%'";
$searchResult = mysql_query($searchSQL);
while ($row=mysql_fetch_assoc($searchResult)){
$results[] = "<div class='webresult'><div class='title'><a href='{$row['url']}'>{$row['title']}</a></div><div class='desc'>{$row['description']}</div><div class='url'>{$row['url']}</div></div>";
}
echo implode($results);
?>
<?php
$database=mysql_connect("localhost","username","password");
mysql_select_db("database");
if(!empty($_GET['q'])){
$query= mysql_real_escape_string(trim($_GET['q']));
$searchSQL = "SELECT * FROM links WHERE `title` LIKE '%{$query}%' LIMIT 0,5";
// .^.
// add a limit clause here. |
$searchResult = mysql_query($searchSQL);
while ($row=mysql_fetch_assoc($searchResult)){
$results[] = "<div class='webresult'><div class='title'><a href='{$row['url']}'>{$row['title']}</a></div><div class='desc'>{$row['description']}</div><div class='url'>{$row['url']}</div></div>";
}
echo implode($results);
}
?>
Solution: Add a LIMIT clause to your query, in order to limit the number of the returned results.
Suggestion: Place your database querying code inside the if clause, because this way you will avoid troubles of the GLOBAL variables kind.
where you have $searchSQL = "SELECT * FROM links WHEREtitleLIKE '%{$query}%'"; you can change this to using a LIMIT clause like so:
$searchSQL = "SELECT * FROM links WHERE `title` LIKE '%{$query}%' LIMIT 5";
For more information on using a LIMIT clause try the following links:
MySQL Reference guide
SELECT * FROM links WHERE `title` LIKE '%{$query}%' LIMIT 0,5"
that should display 5 results.
Im making a PM system.
In the recipient field, if you enter
Fox
It will return: Did you mean Megan Fox?
But what if there is more than Megan, having lastname "Fox", then it will only ask if you meant only one of them.
If there's more than 1 with lastname "Fox" then i wish it to say:
Who did you mean?
Pammy Fox
Megan Fox
And if there only is 1 with that lastname, i wish it to just say
Did you mean Megan Fox?
(like it does now)
How can i do that?
here's my code:
$qur = mysql_query("
SELECT id, firstname, lastname,
(firstname = '$firstname' AND lastname = '$lastname') AS full FROM users
WHERE (firstname = '$firstname' AND lastname='$lastname')
OR (firstname LIKE '$firstname%' AND lastname LIKE '$lastname%')
OR firstname LIKE '$firstname%' OR lastname LIKE '$firstname%'
ORDER BY (firstname = '$firstname' AND lastname='$lastname') DESC");
$get = mysql_fetch_array($qur);
if($get["full"] == 1){
echo $get["id"];
}else{
echo "Did you mean: ".$get["firstname"]." ".$get["lastname"]." ?";
}
while($get = mysql_fetch_array($qur)) {
$name[] = $get["firstname"]." ".$get["lastname"];
}
if(count($name) > 1) {
echo 'Who did you mean?<br/>';
} else {
echo 'Did you mean ';
}
echo implode('<br/>', $name);
use mysql_num_rows($result)
link
while($get = mysql_fetch_array($qur) {
//do your stuff
}
I think anyway, been a long time since I've used mysql with PHP