dimanche 5 mai 2019

Error in if-else conditional when I check a mysql pdo query results

I have a query on mysql using pdo, which returns the number of records found, for this I use COUNT (*) in my query and fetchColumn () to get the information at the end.

What I have tried to do is check if the query got results or not, and print a message on the screen for both cases.

This is my mysql code:

       public static function GetTAKE_OVER_OrdersTotalQueriesCOUNT($connection)
       {
          if (isset($connection)) {
            try {
              $sql = "SELECT COUNT(*) AS num_rows FROM take_over";
              $sentence = $connection ->prepare($sql);
              $sentence -> execute();
              $result = $sentence->fetchColumn();

            } catch (PDOException $ex) {

              print 'ERROR' . $ex -> getMessage();
            }
          }

           return $result;
          }

and this is the code where I call the function:

         $Result20 = OrderModel::GetTAKE_OVER_OrdersTotalQueriesCOUNT(Connection::GetConnection());

                if ($Result20[0][0] !== 0) 
                {
                 echo "There is nothing to display";
                 }
                 else
                 {
                  echo "There is something to display";
                 }

the problem is that both messages are shown. it's as if the conditionals do not work,because it enters the if and the else.

I've tried doing a print_r ($Result20) and it shows me this:

            Array ( [0] => Array ( [num_rows] => 0 [0] => 0 ) )

so, I do not know if I'm doing something wrong in my mysql query or if I'm doing wrong with the if-else conditional.

Can you help me to find the error and solved it?

Aucun commentaire:

Enregistrer un commentaire