I'm getting errors retrieving data from my database. Any help?

I wrote this code to get data from my database and display it to the user that is currently logged in. It works, It displays the card/number of cards, depending on how many rows that user has. The problem now, is that it fails to display the data in each row from the database, instead, it displays an error.

Here is the code for the page.

<section>
    <div class="tab-content">
        <div class="tab-pane active" id="tabs-1" role="tabpanel">
            <div class="row mb-30_reset">
                <?php
                // Replace the placeholders with your actual database credentials
                $dbhost = 'localhost';
                $dbname = 'xclugsn';
                $dbusername = 'root';
                $dbpassword = '';

            
                try {
                    // Establish the database connection
                    $pdo = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbusername, $dbpassword);
                    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                    // Get the username from the session
                    $username = $_SESSION['username'];

                    // Prepare the query
                    $query = "SELECT * FROM nft WHERE nftby = :username";
                    $stmt = $pdo->prepare($query);
                    $stmt->bindParam(':username', $username);
                    $stmt->execute();

                    // Fetch all the rows that match the condition
                    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

                    // Display the data to the user
                    foreach ($rows as $row) {
                        // Access the columns of each row using their names
                        $fileURL = $row['fileURL'];
                        $fileName = $row['fileName'];
                        $fileType = $row['fileType'];
                        $filePrice = $row['filePrice'];

                        // Generate the HTML for each section/card dynamically
                        ?>
                        <div class="col-xl-4 col-lg-6 col-md-6">
                            <div class="card__item three">
                                <div class="card_body space-y-10">
                                    <div class="card_head">
                                        <img src="<?php echo $fileURL !== null ? $fileURL : "No data found"; ?>" alt="">
                                        <a href="#" class="likes space-x-3">
                                            <i class="ri-heart-3-fill"></i>
                                            <span class="txt_sm">1.2k</span>
                                        </a>
                                        <div class="action">
                                            <a href="<?php echo $fileURL !== null ? $fileURL : "No data found"; ?>" class="btn btn-primary btn-sm btn_auction">
                                                <i class="ri-auction-line color_white"></i>
                                                View
                                            </a>
                                        </div>
                                    </div>
                                    <h6 class="card_title">
                                        <a class="color_black" href="<?php echo $fileURL !== null ? $fileURL : "No data found"; ?>">
                                            <?php echo $fileName !== null ? $fileName : "No data found"; ?>
                                        </a>
                                    </h6>
                                    <div class="card_footer d-block space-y-10">
                                        <div class="d-flex justify-content-between align-items-center">
                                            <div class="creators space-x-10">
                                                <div class="avatars -space-x-20">
                                                    <a href="https://xclusivedesign.io/explore/index.php">
                                                        <img src="/static/assets/img/avatars/avatar_1.png" alt="Avatar" class="avatar avatar-sm">
                                                    </a>
                                                    <a href="https://xclusivedesign.io/explore/index.php">
                                                        <img src="/static/assets/img/avatars/avatar_4.png" alt="Avatar" class="avatar avatar-sm">
                                                    </a>
                                                </div>
                                                <a href="<?php echo $fileURL !== null ? $fileURL : "No data found"; ?>">
                                                    <p class="color_blue txt_sm" style="text-transform: uppercase;">
                                                        <?php echo $fileType !== null ? $fileType : "No data found"; ?>
                                                    </p>
                                                </a>
                                            </div>
                                            <a href="<?php echo $fileURL !== null ? $fileURL : "No data found"; ?>" class="space-x-3">
                                                <p class="color_green txt_sm">
                                                    <?php echo $filePrice !== null ? $filePrice : "No data found"; ?> ETH
                                                </p>
                                            </a>
                                        </div>
                                        <div class="hr"></div>
                                        <div class="d-flex align-items-center space-x-10">
                                            <i class="ri-vip-crown-line"></i>
                                            <p class="color_red txt_sm" style="width: auto">
                                                STATUS:
                                            </p>
                                            <span class="color_black txt_sm">
                                                For Sale
                                            </span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php
                    }
                } catch (PDOException $e) {
                    // Handle any potential database connection errors
                    echo "Database Connection Error: " . $e->getMessage();
                }
                ?>
            </div>
        </div>
    </div>

For every place where there’s a code like this <?php echo $fileURL !== null ? $fileURL : "No data found"; ?>, I want it to display the url in the database for each row.

Here’s the error i get.

Please, can you help me out?

What error?

So this

is null or not set. Are you sure there is a table column with this name? Thats why you should never use

SELECT *

because how should we know what is returned by your query if even you do not know?

Are you sure you’ve pulled the right table at all? Cause none of your values are getting set…

1 Like

What is in $rows, when you ran var_dump($rows) as your first debugging step?

Slightly off topic, but storing these items by username when presumably your users table has a unique user ID column may come back to haunt you. It would be better, in my opinion, to store by the user ID.

Presumably the display showing multiple “tiles” suggests that the table contains at least two rows that contain the username, as the foreach must have at least two iterations.

There is a column with that name.

It worked for just one, but now i eant it to display all fir that user.

I’m not trying to retrieve it using the user ID as there’s no way to identify it in the table “nft”.

Yes, the table akd database connection are correct.

That is not the issue.

Have you stuffed this table with actual data?
As droop has highlighted, what comes out when you var_dump $rows? (or even $row, for that matter)

Yes, the rable is stuffed with actual data.

You have been asked twice now

You are requesting data from table nft where the user is named nftby and other columns are fileURL? I don’t trust that.

Show us your database structure or do the var_dump like told multiple times before

And yet it finds two rows, if the posted screen-shot is anything to go by.

When I made use of “var_dump($rows)”, I got this error.

array(2) { [0]=> array(12) { ["nftid"]=> string(2) "39" ["filename"]=> string(10) "Hard Drugs" ["url"]=> string(56) "https://xclusiv/user/uploads/64a2e1f77faea.jpg" ["nftby"]=> string(6) "Mathew" ["fileprice"]=> string(4) "6.00" ["filedescription"]=> string(24) "A flyer about hard drugs" ["filesize"]=> string(1) "2" ["fileprop"]=> string(24) "A flyer about hard drugs" ["filetype"]=> string(3) "art" ["fileroyal"]=> string(2) "90" ["filecoll"]=> string(14) "Dazzling Earth" ["filebid"]=> string(3) "Yes" } [1]=> array(12) { ["nftid"]=> string(2) "44" ["filename"]=> string(7) "Bahamas" ["url"]=> string(67) "https://xclusiv/translated/user/uploads/64a3e6b233143.jpg" ["nftby"]=> string(6) "Mathew" ["fileprice"]=> string(4) "6.00" ["filedescription"]=> string(24) "Bahamas Drivers license " ["filesize"]=> string(1) "1" ["fileprop"]=> string(24) "A flyer about hard drugs" ["filetype"]=> string(3) "art" ["fileroyal"]=> string(3) "100" ["filecoll"]=> string(18) "Light of the world" ["filebid"]=> string(3) "Yes" } }

this is not “fileURL” and “fileName”
So when you were asked, specifically and directly, if the columns “fileURL” and “fileName” existed, and you said yes…

1 Like

That’s not an error, that’s the intended output for var_dump(). As above, you just have your column names wrong. I’m not sure how that could have worked when you were displaying a single row, though.

They are function names.

$amount = getAmountForLoggedInUser();
$rowCount = getCountOfRowsForLoggedInUser();
$fileName = getFileName();
$fileURL = getFileURL();
$fileType = getFileType();
$filePrice = getFilePrice();

But you don’t call or define those functions anywhere in your code.

Function name != column name.

You were asked about column names. Not function names, not… anything else.

Since… apparently you dont know the structure of your table… these are the column names in that table:

nftid
filename
url
nftby
fileprice
filedescription
filesize
fileprop
filetype
fileroyal
filecoll
filebid

Referencing them is case-sensitive.

Thank you all. It was my mistake. I failed to realize that the function names were no longer in use and therefore not changing to the column name in the database.

I switched to the database column names and now everything is working fine.

Thanks, once again. I appreciate the help.

It is fixed.