How to display a section of my webpage to a logged in user if his data is on the database table, and display a message if no data was found

i want a section (nft-section.php) in my users dashboard to display if the currently logged in user’s username is the same as the data in the “nftby” column of the table “nft”. So, I wrote this code

<?php

// Get the currently logged-in user's username from the session
$userName = $_SESSION['username'];

// Prepare the SQL statement
$query = "SELECT * FROM nft WHERE nftby = ?";

// Prepare the statement
$stmt = $conn->prepare($query);

if ($stmt === false) {
    // Error handling: Display the error message
    echo "Error preparing the SQL statement: " . $conn->error;
    exit;
}

// Bind the username parameter
$stmt->bind_param("s", $userName);

// Execute the query
$result = $stmt->execute();

if ($result === false) {
    // Error handling: Display the error message
    echo "Error executing the SQL statement: " . $stmt->error;
    exit;
}

// Get the result
$result = $stmt->get_result();

if ($result === false) {
    // Error handling: Display the error message
    echo "Error retrieving the result set: " . $stmt->error;
    exit;
}

// Check if any matching records were found
if ($result->num_rows > 0) {
    // Display the section of the website for the user
    include 'nft-section.php';
} else {
    // Display "No NFTs Yet"
    echo "No NFTs Yet";
}

// Close the statement and database connection
$stmt->close();
$conn->close();
?>

and it isn’t displaying the section, or the error messages. But when I make use of only the

<?php
    include 'nft-section.php';
?>

it displays the section.

Please, how do I fix this?

Where do you connect to the database? Is your session variable correct?

In your previous thread you were using the much simpler and more modern PDO database extension. In this thread you are using statement from the mysqli database extension, which would be producing fatal php errors, since you cannot mix statements for the two different extensions.

Do you have php’s error_reporting set to E_ALL and display_errors set to ON, preferably in the php.ini on your development system, so that php would help you by reporting and displaying all the errors it detects? Stop and start your web server to get any changes made to the php.ini to take effect and use a phpinfo(); statement in a .php script to confirm that the settings actually took effect.

Sorry bout the late reply @droopsnoot , this has been fixed.

Thanks for your help.

But, I’m currently facing a different issue. Please, can you take a look at the question in my profile?

Thanks, once again.

Sorry bout the late reply @mabismad , this has been fixed.

Thanks for your help.

But, I’m currently facing a different issue. Please, can you take a look at the question in my profile?

Thanks, once again.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.