0

I am using simple select query in MySQL for fetching total number of managers exists in both tables. It works fine when there are managers present in DB and it return multidimensional array.

But when there is no manager exist. Even then it returns a multidimensional array with all values as null. I want it to return null or false or empty if there are not records exist.

CODE:

$query = "SELECT
                            mpp.ManagerWeeklyPointId,
                            mpp.ManagerId,
                            manager.ManagerName,
                            manager.ManagerLastName,
                                SUM(
                                    mpp.Point
                                ) AS Points
                        FROM
                            managerpredictorpoint as mpp
                        INNER JOIN manager ON mpp.ManagerId = manager.ManagerId
                        WHERE
                        manager.Verified = 1 AND
                        manager.Blocked = 0
                        ORDER BY Points DESC,manager.ManagerId ASC
                        LIMIT $offset, $limit";

$obj = $GLOBALS['DBConnect']->prepare($query);
        $executeResult = $obj->execute();
        $response2 = $obj->fetchAll(PDO::FETCH_ASSOC);
var_dump($response2);exit;

OUTPUT:

array (size=1)
  0 => 
    array (size=5)
      'ManagerWeeklyPointId' => null
      'ManagerId' => null
      'ManagerName' => null
      'ManagerLastName' => null
      'Points' => null
4
  • you are using mysqli right? Commented Aug 7, 2015 at 8:02
  • I am using simple mysql with PDO Commented Aug 7, 2015 at 8:04
  • @Gunaseelan var_dump($executeResult) returns true for 0 records Commented Aug 7, 2015 at 8:16
  • Then follow this code. $executeResult = $obj->execute(); if($obj->rowCount()!=0){ $response2 = $obj->fetchAll(PDO::FETCH_ASSOC); var_dump($response2);exit;}else{ echo "No Manager Exists"} Commented Aug 7, 2015 at 8:20

3 Answers 3

0

I think this is because you use sum() function. Look at the example http://sqlfiddle.com/#!9/61816/2 :

select id from t1 where c=2;
select id, sum(c) from t1 where c=2

first select returns nothing but the second returns row with nulls

write it so

select id, sum(c) s from t1 where c=2 having not isnull(s)
Sign up to request clarification or add additional context in comments.

1 Comment

But how can I resolve it? Because I need sum of points of that particular manager
0

Add a NOT NULL condition in your query.

WHERE
                        manager.Verified = 1 
                      AND  
                      manager.ManagerName IS NOT NULL
                      AND
                        manager.Blocked = 0

This shall not give you the null records.

1 Comment

it still gives the same result
0

You have not put the parameters (i.e $offset and $limit) into the execute method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.