querySingle function for PDO
There is a better way.

That's a very good function, but you can make it 100 times more useful by returning the statement.

function pdoQuery($con, $query, $values = array()) {
    if($values) {
        $stmt = $con-&gt;prepare($query);
        $stmt-&gt;execute($values);
    } else {
        $stmt = $con-&gt;query($query);
    }
    return $stmt;
}


Given there are two excellent things,


"method chaining" (if some statement returns an object, you can call this object's method directly by adding -&gt; )
different fetch methods supported by PDOStatement


So in the end your current code will be changed a little, but you will get a lot of other neat ways for getting your data.

To get a row

$row = pdoQuery($pdo, "SELECT name FROM table WHERE id=?", [$id])-&gt;fetch();


to get a name

$name = pdoQuery($pdo, "SELECT name FROM table WHERE id=?", [$id])-&gt;fetchColumn();


But beside these two use cases there are many others!


you can run a DML query as well:

pdoQuery($pdo, "DELETE FROM table WHERE id=?", [$id]);

fetch a list of records

$data = pdoQuery($pdo, "SELECT * FROM table")-&gt;fetchAll();

use all the formats supported by fetchAll()
even get a row count

$deleted = pdoQuery($pdo, "DELETE FROM table WHERE id=?", [$id])-&gt;rowCount();



As you can see, this new function is much more flexible and can be used to ease a lot more tasks