Search within database field - php

I am building a job search site where candidates can register themselves for various jobs . Also employers can post jobs with multiple qualification (eg if he wants MBA or Phd it will be saved in database like mba,phd . I want to show jobs for the registered candidates which are relevant to his educational qualification . So I am confused how can I do this because I think I have to search within database . Please help.
if($cid >0 ) //If the candidate is registered
{
$quali = $this->getCandidatesQualification($cid);
$cond = "WHERE "; //I want to put a condition if he is registered with us , it will show jobs a/c to his qualification
}
UPDATE :
$cond = "";
if($cid >0 )
{
$quali = $this->getCandidatesQualification($cid);
$cond = "WHERE emp_qualification LIKE '%$quali%'";
}
$sql = "SELECT
emp_job_id,emp_job_profie,emp_qualification,emp_experience
FROM
tbl_emp_data
$cond
ORDER BY job_add_date DESC LIMIT 0,10
";
// Query Search all jobs
$sql = "SELECT
emp_job_id,emp_job_profie,emp_qualification,emp_experience
FROM
tbl_emp_data
ORDER BY job_add_date DESC LIMIT 0,10
";

You use the LIKE keyword - for example,
SELECT
emp_job_id,emp_job_profie,emp_qualification,emp_experience
FROM
tbl_emp_data
WHERE emp_qualification LIKE '%MBA%'
ORDER BY job_add_date DESC LIMIT 0,10
Here, the '%' wildcard is used for any number of characters before or after MBA, so it will find it in strings like MBA, BTech, MBA, BCom, MBA, PhD.

set a colum in your tbl_emp_data names 'is_registered' type ENUM and set two value ('0','1')
and then set WHERE condition
WHERE is_registered = 1

Related

PHP select form a mysql table and order by another table

I have 2 tables in my MySQL Database:
customer
customer_billing
customer table has the columns columns
sequence
company
the *customer_billing* table has the columns
sequence
customer_seq
i am running a select query on the *customer_billing* table and then within a while loop a select query in the customer table like this:
$sql="SELECT *, SUM(quantity*unitprice) as customertotal from customer_billing where resellerid = '' and salesmanid = '' and producttype = '".$_GET["producttype"]."' group by customer_seq ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
$sql2="SELECT * from customer where sequence = '".$result["customer_seq"]."' and company_status = '' ";
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
$result2=mysql_fetch_array($rs2);
}
i want to be able to order the results displayed by company ASC from the customer table
i have tried order_by customer_seq by obviously this only orders the sequence numbers
i have also tried doing order by company ASC on the customer table query but this didnt work either
how can i get round this?
You should make a join between customer and customer_billing on customer_seq and add company to the group by clause.
This way you should be able to order it by company.
You can use Joins and no need to use a second query try this,
$sql="SELECT c.*,ct.*, SUM(ct.quantity*ct.unitprice) as customertotal FROM
customer_billing ct , custom c WHERE ct.resellerid = '' AND ct.salesmanid = ''
AND c.company_status = '' AND c.sequence=ct.customer_seq AND
ct.producttype = '".$_GET["producttype"]."'
GROUP BY ct.customer_seq ORDER BY c.company ASC ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
// your code
}
You need to do this in a single query using join. Should be smth like this:
SELECT cb.customer_seq, c.sequence, c.company, SUM(quantity*unitprice) as customertotal
FROM customer_billing AS cb JOIN customer AS c
ON cb.sequence = c.sequence
WHERE cb.resellerid = ''
AND cb.salesmanid = ''
AND cb.producttype = '$_GET["producttype"]'
AND c.company_status = ''
GROUP BY cb.customer_seq, c.company
ORDER BY c.company

PHP/MYSQL SEARCH QUERY RETURNS ERROR: Subquery returns more than 1 row

please I am totally new to mysql, my problem is:
I have a table called 'cong' which has the following columns(id, sort_code, pin, name, state, lga, zip, address, min, min_photo, sec, min_phone, sec_phone) which contains all congregations.
The columns (state, lga) contains the id from the tables 'states' and 'local_govt'.
The 'states' table has the following columns (id, country_id, name), and the 'local_govt' table has the following columns (id, country_id, state_id, name).
I want to carry out a search on the 'cong' table which should search through the 'state' and 'local_govt' tables for matches, below is the search function I wrote:
<?php
function find_cong($term) {
$query = "SELECT * FROM cong";
$query .= " WHERE state rLIKE
(SELECT id FROM states WHERE upper(name) rLIKE '{$term}')";
$query .= " OR lga rLIKE
(SELECT id FROM local_govt WHERE upper(name) rLIKE '{$term}')";
$query .= " OR upper(name) rLIKE '{$term}'";
$query .= " OR upper(address) rLIKE '{$term}'";
$query .= " OR upper(sort_code) rLIKE '{$term}'";
$query .= " OR upper(pin) rLIKE '{$term}'";
$query .= " OR upper(zip) rLIKE '{$term}'";
$query .= " OR upper(min) rLIKE '{$term}'";
$query .= " OR upper(sec) rLIKE '{$term}'";
$query .= " OR upper(min_phone) rLIKE '{$term}'";
$query .= " OR upper(sec_phone) rLIKE '{$term}'";
$result = mysql_query($query);
confirm_query($result);
return $result;
}
function confirm_query($query) {
if (!$query) {
die("Database query failed : " . mysql_error());
}
}
?>
The problem now is that, it searches some terms and comes up with accurate results, but for some specific terms like local_govt and state names it pops an error:
(Database query failed : Subquery returns more than 1 row)
Please I need your help as I don't have any idea how to write the code better than that.
Thanks.
You have subequeries in the states and local_govt portions of the WHERE statement. Presumably there are rows for a given value of $term where those queries will return a resultset of more than one row. Because you are using rLIKE, which expects to evaluate against one value (rather than multiple), the overall query will error out.
Instead, refactor as follows:
$query .= " WHERE state IN (SELECT id FROM states WHERE upper(name) rLIKE '{$term}')";
$query .= " OR lga IN (SELECT id FROM local_govt WHERE upper(name) rLIKE '{$term}')";
this will account for that contingency.
Please note that the query as written is unlikely to be very performant. Since you are scanning many different columns, it would be best to try not to use regex here, because the optimizer won't be able to leverage indices. Ideally, reduce it to a constant, so that you can use:
SELECT * FROM cong WHERE $term IN (upper(name), upper(address)...)
but that may not be possible given your requirements. If that's the case, I would probably look at the design of your program and try to split the query into a lookup against one column at most from the application side, e.g.:
SELECT * FROM cong WHERE $term rLIKE upper(NAME)
There error is here:
$query .= " WHERE state rLIKE
(SELECT id FROM states WHERE upper(name) rLIKE '{$term}')";
$query .= " OR lga rLIKE
(SELECT id FROM local_govt WHERE upper(name) rLIKE '{$term}')";
rlike is a regex, but in the end boils down to a straight comparison of values. Your subqueries are returning multiple rows, so in a re-writen fashion, your query would be something more like:
WHERE state = a,b,c,...
OR lga = z,y,x,...
You're trying to do an equality test against multiple values, which is confusing the database. Equality tests are for SINGLE values, e.g. a=b, not a=b,c.

Query to return certain row always

Can you suggest a query where a condition will be executed always . Say :
if($country == 'hong-kong')
{
$cond = "AND country = 'china'";
}
$q = "
SELECT
*
FROM
tbl_people
WHERE
region = 'asia'
$cond
ORDER BY RAND()
LIMIT 0,15
";
Now suppose $country = 'hong-kong' and it has total 4 members . What I want to know is is there any way so that if $country = 'hong-kong' I wll have 15 members in result set including the 4 members from hong-kong ?
If I understand the question correctly, you want 15 random results from a larger set, but you want to be sure that all of the results matching country = 'hong-kong' are included in those 15.
I think you're going to want two queries, because the number of records returned by the first one (country = 'hong-kong') will dictate the limit you put on the second query. E.g.:
SELECT
*
FROM
tbl_people
WHERE
country = 'hong-kong'
ORDER BY RAND()
LIMIT 0,15
Then
SELECT
*
FROM
tbl_people
WHERE
region = 'asia' AND country <> 'hong-kong'
ORDER BY RAND()
LIMIT 0,X
...where X is 15 - N, where N is how many rows were returned by your first query.
Then of course, you have to combine them in some kind of randomizing way (unless you want the Hong Kong results to be at the top/bottom/whatever of the list).

Search SQL query where this is true

I have a search query that works great, but I need it to only show results where visible = true. That's what needs to be added to the query, I just don't know how to do it correctly. Here's the query.
$query = $mysqli->query("SELECT
id,name,company,town FROM people
WHERE name LIKE '%$mysearchString%' OR
company LIKE '%$mysearchString%' OR
town LIKE '%$mysearchString%' ORDER BY
name DESC LIMIT 100");
The $mysearchString var is just a sanitized $_GET['s'].
SELECT id,name,company,town
FROM people WHERE
(name LIKE '%$mysearchString%'
OR company LIKE '%$mysearchString%'
OR town LIKE '%$mysearchString%')
AND visible = true
ORDER BY name DESC LIMIT 100
$query = $mysqli->query("SELECT id,name,company,town FROM people WHERE (name LIKE '%$mysearchString%' OR company LIKE '%$mysearchString%' OR town LIKE '%$mysearchString%') AND visible = true ORDER BY name DESC LIMIT 100");

MySql return only one instance of duplicate entries

Hi I have a table with names and numbers entered along with other data . The table called events contains many instances where the name and numbers are the same but the other entries are different. I want to perform a search using names so I can display the number. But in my search I only need to return one instance of a name . The table is like this
Name Number Responisble Position
Paul 8455 Chorley t7
Dave 3821 PR south f5
Paul 8455 PR North p9
Paul 8455 Leyland t6
Dave 3821 Ribbleton r4
and my script is this
$condition = "name LIKE 'Paul' ";
$result = mysql_query("SELECT * FROM events WHERE $condition ") ;
while($row = mysql_fetch_array($result)) {
The script is not complete but I hope you can see that the results would return 3 results but what i want is a result with just one for each name
I hope this makes sense ! thanks for any help
$condition = "name LIKE 'Paul' ";
$result = mysql_query("SELECT * FROM events WHERE $condition GROUP BY name") ;
try using distinct -
SELECT DISTINCT (columns) FROM (table) WHERE (condition)
edit probably disregard this, mis-understood your question i think
MySql return only one instance of duplicate entries
You can either use group by or distinct in the select statement see http://dev.mysql.com/doc/refman/5.1/en/select.html
In your example it would be
$condition = "name LIKE 'Paul' ";
$result = mysql_query("SELECT * FROM events WHERE $condition GROUP BY name") ;
while($row = mysql_fetch_array($result)) {
SELECT name, number FROM events WHERE $condition GROUP BY name, number

Categories