Reputation: 440
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
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