mercredi 25 novembre 2020

function for PDO queries and return result - tested - do you has any tips about this to test or check?

I write a function(PHP and MySql) to get PDO connection, query and array of placeholders to execute query and return result

I test this func and has good respond when result for SELECT has empty result or has error, do you see any problems or tips to say about this?

do you has any tips about this to test or check?

function and test code:

function db_query_pdo( PDO $pd, $q,$ar )
    {
            
//      $pd Pdo connection object, $q query string ,  $ar array of placeholders
      try
       {
            $stmt = $pd->prepare($q);
            $x=$stmt->execute($ar);
            $rs=$stmt->fetchAll();
            
            if(!empty($rslt))
                return $rslt;
            else
                return $x;
        }
        catch (Exception $ex)
        {
            echo 'Query failed';
//            exit(); uncomment this lines if needed in future
 
//    echo $ex->getCode();
//    echo $ex->getMessage();
//    throw $ex;
        }
 
    }
// TEST CODE
$pdo_cnctn=db_con_pdo(); // is a pdo connection function that worked correctly and return connection object
    $id=133;
    $id2=77;
    
$query='SELECT * FROM `users` WHERE u_id=:uid OR u_id=:uid2;
';
$vals=[
    'uid' => $id,
    'uid2' => $id2
];
 
    $res= db_query_pdo($pdo_cnctn,$query,$vals);
//$res[] = $stmt->fetch();
    if(!is_bool($res))
    {
        var_dump($res);
    }
    else
    {
       // $$pdo_cnctn->rollBack();
        echo 'Not Found';
        goto  end_point;
    }
    
    //some codes
    end_point:

Aucun commentaire:

Enregistrer un commentaire