RedStar Entertainment
RedStar Entertainment

Reputation: 440

php Notice: Trying to access array offset on value of type null in

I have a function which I created to return all fields of a row. However, it returns the following error.

Notice: Trying to access array offset on value of type null in

My Code

function returnAllData($table, $field, $value){
  global $pdo;

  $stmt = $pdo->prepare("SELECT * FROM $table WHERE $field = :val");
  $stmt-> bindValue(':val', $value);
  $stmt-> execute();
  $f = $stmt->fetch();
}
$memData = returnAllData('members', 'mem_id', userId());
echo $memData['mem_phone'];

I checked again and again but I could not get what actually is wrong with the code.

Upvotes: 0

Views: 615

Answers (1)

Ro Achterberg
Ro Achterberg

Reputation: 2704

You forgot to return the result from the fetch() call. Try this:

function returnAllData($table, $field, $value){
  global $pdo;

  $stmt = $pdo->prepare("SELECT * FROM $table WHERE $field = :val");
  $stmt-> bindValue(':val', $value);
  $stmt-> execute();
  $f = $stmt->fetch();
  return $f;
}
$memData = returnAllData('members', 'mem_id', userId());
echo $memData['mem_phone'];

Upvotes: 2

Related Questions