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?