K
K
Kolya Vantukh2016-11-01 17:24:09
PHP
Kolya Vantukh, 2016-11-01 17:24:09

How to return the result of a query in pdo through a function?

There is a function that makes a query to the database

function queryRes($qury_start_num,$sort,$type) {
        $pdo = getConnection();
        $result = $pdo->query("SELECT * FROM table_products ORDER BY $sort $type $qury_start_num ");
             return $result;
}

How to correctly pass the query query value so that you can use it further like this
//возвращается результат
queryRes($qury_start_num,$sort,$type);
if($result->fetchColumn() > 0) {
     
     while ( $res = $result->fetch()) {
    
     echo $res['title'].'<br/>';
     }
       }

Because in this way the result is not returned

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2016-11-01
@ronik55

Use classes instead of this not the best code...
Use this wrapper phpfaq.ru/pdo/pdo_wrapper

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question