PHP - 无法使用数据库中的数据和内部服务器错误

I'm trying to create a website (similar to pastebin) that allows a user to submit text on the homepage, which then takes them to a randomly generated url (domain.com/ABCXYZ) that is running run.php, which will then display the original content they submitted.

Once a user submits the form their input userentry is added to the database (column userinput). The randomly generated url code idgen (so a random code like KmdpV) is added to the randomurl column in the database.

This much is working without issue (successfully takes users to a randomly generated URL and the adds the userentry and idgen to the database.

However, two issues are popping up that I can't seem to solve:-

  1. I keep getting a 500 internal server error (seems to be caused by something within the else if (isset...) statement in the run.php code below. The error it keeps giving is GET http://example.com/WsnNp 500 (Internal Server Error) (where WsnNp is any randomly generate code) Failed to load resource: the server responded with a status of 500 (Internal Server Error).
  2. For some reason, I can't seem to access the userentry data at all. Once the form is submitted, it is succesfully added to the database and succesfully takes me to a random url running run.php, but it won't display anything if I write <?php echo $_GET['userentry'] ?> (nor using POST, nor using SELECT ... FROM database etc.).

Being able to access the userentry on the randomly generated page is key for the entire site. The userinput needs to be permenantly available on that randomly generated URL, so for example, if I have the database row:

ID | userinput | randurl | 102 | Hello world | GdksQ |

Then I'm trying to make it so that if a user visits example.com/GdksQ then it would display Hello world

I've already attempted grabbing the data from the database etc., but all my attempts seem to lead back to the 500 internal server error.

<?php $endurl = $_POST['idgen']; ?>
<?php
if (isset($_POST['userentry'])) { 
    // servername, username, password, dbname here
    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "INSERT INTO mydatabasename (userinput,randurl)
    VALUES ('$_POST[userentry]','$_POST[idgen]')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
    $conn->close();  
    header('Location: http://example.com/'.$endurl);
}

The if part of this statement (above) seems to be working, because as mentioned, it does insert the values into the database, and it does take you to a random url after you submit the form (but I still can't display userentry nor idgen, even if I write it outside of this statement.

else if (isset($_GET['idgen'])) {
    // servername, username, password, dbname here
    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT userinput FROM mydatabasename WHERE randurl = '".$_GET[idgen]."'";

    $result = $db->query($sql);
    if ($result) {
            if ($row = $result->fetch_object()) {
            echo($row['userinput']); }
        $result->close() ; }
  $conn->close(); } ?>

As far as I've been able to narrow it down, it's the above else if statement that's causing the 500 internal server error. Although I'm not sure if this is also what's preventing me from displaying / using the userinput?

<form action="/run.php" method="POST">
    <input type="hidden" name="idgen" value="<?php echo $randomGen ?>">
    <input type="text" name="userentry">
    <input type="submit">
</form>

($randomGen is just a function that generates a random 5 letter id for use in the url)

RewriteRule ^([0-9a-zA-Z]+)$ run.php?idgen=$1 [L]

This is the RewriteRule that allows me to use random urls and still run run.php on those pages.

EDIT:

Having checked the error logs, it seems the issue is stemming from this line: $result = $db->query($sql); as it says PHP Fatal error: Call to a member function query() on null in /home/mycpanelname/public_html/run.php on line 46.

There's also some PHP Notices, but mainly seem to be undefined variables etc.