lundi 21 septembre 2015

I face error in my IF ELSE

I am new in PHP. I face problem in my IF ELSE. My IF ELSE Statement is not working. I have two sql queries. In starting i have a simple query which fetch client_id against opinion_id and store the result of thi squery in a variable $opinion_id

$opinion_id = "(SELECT `client_id` FROM `pacra_client_opinion_relations` WHERE `opinion_id` = 379)";

After that i have my sql query which fetch the all opinion_id against client_id which is fetched from previous query.

 $q_opinion="SELECT r.client_id,c.id,t.id,a.id,o.id,c.name as opinion, r.notification_date, t.title as ttitle,a.title as atitle,o.title as otitle, l.title as ltitle, s.title as stitle, pr.opinion_id, pc.id, pr.client_id as pr_client, pc.address, pc.liaison_one, city.id, pc.head_office_id, city.city, pc.title as cname
FROM og_ratings r 
    inner join
(
  select max(notification_date) notification_date,
    client_id
  from og_ratings
  group by client_id
  ) r2
  on r.notification_date = r2.notification_date
  and r.client_id = r2.client_id
LEFT JOIN og_companies c
ON r.client_id = c.id
LEFT JOIN og_rating_types t
ON r.rating_type_id = t.id
LEFT JOIN og_actions a
ON r.pacra_action = a.id
LEFT JOIN og_outlooks o
ON r.pacra_outlook = o.id
LEFT JOIN og_lterms l
ON r.pacra_lterm = l.id
LEFT JOIN og_sterms s
ON r.pacra_sterm = s.id
LEFT JOIN pacra_client_opinion_relations pr
ON pr.opinion_id = c.id
LEFT JOIN pacra_clients pc
ON pc.id = pr.client_id
LEFT JOIN city
ON city.id = pc.head_office_id
WHERE r.client_id  IN (SELECT opinion_id FROM pacra_client_opinion_relations WHERE client_id = $opinion_id)
";

My 3rd query is for that if there is no client_id against opinion_idwith reference to my 1st query than it only print the record against thatopinion_id`

    $q_opinion1 = "SELECT r.client_id,c.id,t.id,a.id,o.id,c.name as opinion, r.notification_date, t.title as ttitle,a.title as atitle,o.title as otitle, l.title as ltitle, s.title as stitle, pr.opinion_id, pc.id, pr.client_id as pr_client, pc.address, pc.liaison_one, city.id, pc.head_office_id, city.city, pc.title as cname
FROM og_ratings r 
    inner join
(
  select max(notification_date) notification_date,
    client_id
  from og_ratings
  group by client_id
  ) r2
  on r.notification_date = r2.notification_date
  and r.client_id = r2.client_id
LEFT JOIN og_companies c
ON r.client_id = c.id
LEFT JOIN og_rating_types t
ON r.rating_type_id = t.id
LEFT JOIN og_actions a
ON r.pacra_action = a.id
LEFT JOIN og_outlooks o
ON r.pacra_outlook = o.id
LEFT JOIN og_lterms l
ON r.pacra_lterm = l.id
LEFT JOIN og_sterms s
ON r.pacra_sterm = s.id
LEFT JOIN pacra_client_opinion_relations pr
ON pr.opinion_id = c.id
LEFT JOIN pacra_clients pc
ON pc.id = pr.client_id
LEFT JOIN city
ON city.id = pc.head_office_id
WHERE r.client_id  IN (SELECT client_id FROM og_ratings WHERE client_id = 379)";

For that purpose i use IF ELSE Statement

    if($opinion_id == NULL){
    $query = $q_opinion1;
    }
    else
    { 
        $query = $q_opinion;
    }
  $result1 = mysql_query($query) or die;

In My IF ELSE I have a problem it always execute my ELSE part if my client_id is null or not.

Aucun commentaire:

Enregistrer un commentaire