Okay so I didn't quite know how to word the question but, I am working on a project and part of the project is supposed to display a history of results for a specific user(The user being a column in the table). My end goal: The user has been banned multiple times and I want to display all of the bans for that specific user. But I can't quite figure out how to do this.
This is the part of code where I want to display the results:
<?php
echo '<li>
<div>
<p>'.$row['banDate'].'</p>
<p>'.$row['banReason'].'</p>
<p>'.$row['banLength'].'</p>
</div>
</li>';
}
?>
And I want the result of these rows to be related to .$row['pName']. Here is my sql Query as well:
$db = new PDO("mysql:host=".DBHOST.";port=3306;dbname=".DBNAME, DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = ('SELECT UUID, pName, isBanned, pRank FROM bans WHERE pName = :pName');
$stmt_1 = $db->prepare($query);
$stmt_1->execute(array(':pName' => $_GET['name']));
$row = $stmt_1->fetch();
Ignore the WHERE that is to view each user.
The bans will be stored in a SQL database connected to a Minecraft server. I have tried everything but am still at a dead end.
Well the columns you are trying to use you dont have in your select list so first you need to adjust your query:
SELECT UUID, pName, isBanned, pRank, banLength, banReason, banDate FROM bans WHERE pName = :pName
Then to output all the rows you need to loop over your results or call fetchAll and loop over the array. I would loop over the statment result though since there could potentially be a lot of results and you dont seem to have a reason for fetching them all up front:
<?php
$db = new PDO("mysql:host=".DBHOST.";port=3306;dbname=".DBNAME, DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$stmt = $db->prepare('SELECT UUID, pName, isBanned, pRank, banLength, banReason, banDate FROM bans WHERE pName = :pName');
$stmt->execute(array(':pName' => $_GET['name']));
} catch (PDOException $e) {
// do something if you have an exception thrown
echo $e->getMessage();
exit();
}
?>
<?php if ($stmt->rowCount() < 1): ?>
<p>No bans for user</p>
<?php else: ?>
<ul>
<?php while (false !== ($row = $stmt->fetch(PDO::FETCH_ASSOC))): ?>
<li>
<div>
<p><?php echo $row['banDate'] ?></p>
<p><?php echo $row['banReason'] ?></p>
<p><?php echo $row['banLength'] ?></p>
</div>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>