# my concept ( new PDO FETCH MODE )

- Thread: https://externals.io/message/99002
- Original archive: https://news-web.php.net/php.internals/99002
- Started by Csonka Titus on 2017-05-10T15:51:45+00:00
- Messages: 2
- RFC: Nullable Types (Implemented (PHP 7.1)) — https://externals.io/rfc/nullable_types.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #99002 — Csonka Titus — 2017-05-10T15:51:45+00:00

URL: https://externals.io/message/99002#99002

Greetings,

I want to propose a new PDO FETCH MODE.

Whenever I fetch(http://php.net/manual/en/pdostatement.fetch.php) a class, it  returns the new instance of the requested class or false in any other cases.

However with the new php7.1 return type declaration(https://wiki.php.net/rfc/nullable_types) Upon defining a nullable class as return type, I need to add extra code at the end of my function if my database don't have a row to return.

PDO will return a false, my function will trigger a fatal error.

My proposal is to add a new style of fetch(PDO::FETCH_NULL_IFNODATA), returning null if the PDOStatement::fetch has no result.

//this function will trigger a fatal error if the user is not found
function getUserByName($name): ?UserClass{
    $pdo = new PDO(dsn, user, pass, driver_options);
    $sql = "SELECT * FROM user WHERE name = {$pdo->quote($name)}";
    $stmt = $pdo->query($sql);
    $stmt->setFetchMode(PDO::FETCH_CLASS,'UserClass');

    return $stmt->fetch();
}

//this function will return null representing the absent of user
function getUserByName($name): ?UserClass{
    $pdo = new PDO(dsn, user, pass, driver_options);
    $sql = "SELECT * FROM user WHERE name = {$pdo->quote($name)}";
    $stmt = $pdo->query($sql);
    $stmt->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_NULL_IFNODATA,'UserClass');

    return $stmt->fetch();
}

Looking forward to you answer

Best regards
Miklos Csonka

---

## #99003 — Ryan Pallas — 2017-05-10T16:12:15+00:00

URL: https://externals.io/message/99002#99003 · In reply to: #99002

Couldn't you just use

return $stmt->fetch() ?: null;

to return null instead of false?



> Greetings,
>
> I want to propose a new PDO FETCH MODE.
>
> Whenever I fetch(http://php.net/manual/en/pdostatement.fetch.php) a
> class, it  returns the new instance of the requested class or false in any
> other cases.
>
> However with the new php7.1 return type declaration(https://wiki.php.
> net/rfc/nullable_types) Upon defining a nullable class as return type, I
> need to add extra code at the end of my function if my database don't have
> a row to return.
>
> PDO will return a false, my function will trigger a fatal error.
>
> My proposal is to add a new style of fetch(PDO::FETCH_NULL_IFNODATA),
> returning null if the PDOStatement::fetch has no result.
>
> //this function will trigger a fatal error if the user is not found
> function getUserByName($name): ?UserClass{
>     $pdo = new PDO(dsn, user, pass, driver_options);
>     $sql = "SELECT * FROM user WHERE name = {$pdo->quote($name)}";
>     $stmt = $pdo->query($sql);
>     $stmt->setFetchMode(PDO::FETCH_CLASS,'UserClass');
>
>     return $stmt->fetch();
> }
>
> //this function will return null representing the absent of user
> function getUserByName($name): ?UserClass{
>     $pdo = new PDO(dsn, user, pass, driver_options);
>     $sql = "SELECT * FROM user WHERE name = {$pdo->quote($name)}";
>     $stmt = $pdo->query($sql);
>     $stmt->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_NULL_
> IFNODATA,'UserClass');
>
>     return $stmt->fetch();
> }
>
> Looking forward to you answer
>
> Best regards
> Miklos Csonka
