Valentoni
Valentoni

Reputation: 328

Check if PDO query is returning data or boolean

I am developing a database class, which also performs data arrangement in case of queries that returns data sets. My issue is: i use one single method to perform all queries, but PDO always returns true or false on execute and i don't find a way to check if i have to fetch data and return it or simply return the execute result.

What happens is that when it is a select statement, for example, it shall return fetched data,but when it is a non-returning-data query, like update, for example, it simply shall return the boolean execute result.

Here's the method:

<?php
    public function query($sqlobj) {
        $stmt = $this->prepare_statement($sqlobj->sqlstring,array_values($sqlobj->sqlvalues));
        $res = $stmt->execute();

        if ([$res IS A DATASET]) {
           $res = $stmt->fetchAll();
        }

        return $res;
    }
?>

On mysqli, it was pretty simple, cause query returns false for failure, true for successful queries which doesn't return data(ex.: update statements) and a data set resource for successful queries that brings data from DB(SELECT, COUNT, DESCRIBE, etc.), so it was a simple check:

<?php
    if($res !== true && $res !== false){
        // fetch results
    }
?>

Is there a way to perform this check on PDO, too? I did a lot of research and found things about PDOStatement::rowCount and also somethings about using PDOStatement::fetch() and checking if it is returning false. This second appeared more interesting, but it doesn't solve the problem, yet, because i will get false for non-data-returning queries as well for returning-data queries with empty results. Any brilliant ideas, folks?

Upvotes: 0

Views: 3040

Answers (2)

Valentoni
Valentoni

Reputation: 328

On comments, the user @NiettheDarkAbsol gave me the correct answer and as he didn't post the answer itself, i will do it.

The question is: how can i check it queries results is fetchable or not, if all the info PDO gives me is true for success or false for failure? The answer is PDOStatement::columnCount() method:

<?php
    public function query($sqlobj) {
        $stmt = $this->prepare_statement($sqlobj->sqlstring,array_values($sqlobj->sqlvalues));
        $res = $stmt->execute();

        if ($stmt->columnCount() > 0) {
           $res = $stmt->fetchAll();
        }

        return $res;
    }
?>

As he said, columnCount() will returns me the number of columns of a query result set, even when it is empty. If query returns a non-fetchable result, columnCount() will returns 0.

Thank you @NiettheDarkAbsol!

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133360

public function query($sqlobj) {
    $stmt = $this->prepare_statement($sqlobj->sqlstring,array_values($sqlobj->sqlvalues));
    $res = $stmt->execute();

    if ($res ) {
       $result  = $stmt->fetchAll();
      foreach ($result as $key => $row) {
          echo $link['your_column1'];
      }
    }

}

Upvotes: 0

Related Questions