I am running a php script which will insert a new record into a database within a function and after it has been inserted I wanted to get all contents from the same database using a second function but I get the following error

> 
> Fatal error: Uncaught Error: Call to a member function prepare() on
>  null in /www1/fe5/www.seansdesigns.co.uk/web/foodApp/test.php:33 Stack
>  trace: #0 /www1/fe5/www.seansdesigns.co.uk/web/foodApp/test.php(9):
>  getData() #1 {main} thrown in
>  /www1/fe5/www.seansdesigns.co.uk/web/foodApp/test.php on line 33
> 
> 
> 

If I call both functions individually it works so the sql is working OK.

```
<?php

$test = "update";

if ($test == "update"){
    insertWeight();
    getData();
}

function insertWeight(){
    require_once "connect.php";

    $username = 'sean14';
    $weight = '60';
    $date = '20-01-03';

    $sql = "INSERT INTO $username (date, weight) VALUES (:date, :weight)";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(":weight", $weight);
    $stmt->bindParam(":date", $date);
    $stmt->execute();
    $stmt = null;
}

function getData(){
    require_once "connect.php";

    $username = 'sean14';

    $stmt = $pdo->prepare("SELECT * FROM $username");
    $stmt->execute();
    $stmt = null;
    $x = 0;
    while ($row = $stmt->fetch()){
        $x++;
        $items[$x] = $row;
    }
    echo json_encode ($items);
}
?>

```