Suraj Hazarika
Suraj Hazarika

Reputation: 667

Mysql query advice/help needed

I am building a site where candidates can post their resume and employers can post their jobs.

The employers can post a job with multiple qualifications and I saved it in database like this PHP,Javascript,ASP. Now I want admin to be able to select the candidates who are eligible for a post.

I have written the query:

$sql = "
        SELECT
            cand_f_name,
            cand_l_name,
            cand_qualification,
            cand_phone,
            cand_email,
            cand_experience_yr,
            cand_experience_mn,
            cand_message,
            cand_id
        FROM
            tbl_cand_data
        WHERE
            cand_qualification LIKE '%$emp_q%' 

But it is not showing the expected result. I think my condition cand_qualification LIKE '$emp_q' is wrong.

My tbl_cand_data :enter image description here

Upvotes: 0

Views: 72

Answers (3)

Mike Purcell
Mike Purcell

Reputation: 19979

If you are doing a LIKE query you should include wildcards, as they will match a string containing, otherwise just do an exact match using =:

// String match
WHERE
    cand_qualification LIKE '%emp_q%';

// Exact match
WHERE
    cand_qualification = '$emp_q';

// You could try a WHERE IN clause as well
WHERE cand_qualification IN ('$emp_q');

// Values have to be quoted individually
WHERE cand_qualification IN ('BA','BSc','BCom');

// If you have an array you can do this:    
$myArray = array('BA', 'BSc', 'BCom');
$emp_q = "'" . implode("','", $myArray) . "'"; //Output 'BA', 'BSc', 'BCom'

Upvotes: 2

Your Common Sense
Your Common Sense

Reputation: 157839

I saved it in database like this PHP,Javascript,ASP

That's what you did utterly wrong.

you have to create a related table (that's why our ratabase called relational one!) storing qualifications, and interconnection table, consists of qualifications id linked with candidates ids.
And query them using basic joins.

Note that despite of your current decision, even if you decide to continue with your lame schema, you WILL have to remake it proper way, sooner or later (but sooner will make you less work).
That is the very basics of database architecture and noone can go against it.

SELECT fields FROM tbl_cand_data d, qualification q, qual_cand qc 
WHERE q.name = 'ASP' AND q.id=qc.qid AND d.id=qc.did

Upvotes: 2

MOHAMED FATHEI
MOHAMED FATHEI

Reputation: 468

Like requires percent % try it

Upvotes: -1

Related Questions