Search code examples
phppdochaining

Chaining PHP PDO query


I'm not sure if "chaining" is the correct term for this but what I'm asking is if its possible to make a PDO query similar to this MySQLi query...

$sql = mysqli_fetch_object($db->query("SELECT username FROM member WHERE userID = 1");

with PDO I have only been able to do it this way

$sql = $db->query("SELECT username FROM member WHERE userID = 1");
$query = $sql->fetch(PDO::FETCH_OBJ);

is it possible to "chain" (please correct me if there is a better term for that) the query with PDO or no?


Solution

  • Have you tried

    $query = $db->query("SELECT username FROM member WHERE userID = 1")->fetch(PDO::FETCH_OBJ);