PHP MySQL novice to ninja 6th edition – getJoke function (p.228)

In the book on page 228 we have getJoke function:

function getJoke($pdo, $id) {

$query = $pdo->prepare('SELECT FROM `joke`

WHERE `id` = :id');

$query->bindValue(':id', $id);

$query->execute();

return $query->fetch();

}

As I know functions receive a return value as a result.

Can anyone explain what kind of value aforementioned function return after executing this line of code: return $query->fetch();

Try adding this DEBUG script:

function getJoke($pdo, $id) {

$query = $pdo->prepare('SELECT FROM `joke`

WHERE `id` = :id');

$query->bindValue(':id', $id);

$query->execute();

$result = $query->fetch(); 

// DEBUG START ======================================
  echo '<pre>'; // ADD LINEFEEDS
    echo gettype( $result );
    echo '<hr>';
 
   echo print_r( $result );
  echo '<hr>';
  
  echo var_dump( $result );
  echo '</pre>';

  die; // halts execution

// DEBUG END =======================================

return $query->fetch();
}

Edit:
Search for PHP gettype()
and select the online PHP Manual:

https://www.php.net/manual/en/function.gettype.php

1 Like

As it’s returning the value returned by the fetch function, you can look at the documentation for that to see what kind of value is returned: https://www.php.net/manual/en/pdostatement.fetch.php

2 Likes