I'm very new to PHP and Mysql I'm writing shopping website for my assignment,
I have my login, registration, and database up and running, and now I'm at the stage that needs to do my User profile page that is required to pull data out from database,
firstly I have this code working as it will pull everything from my table,
<?php
$sql = 'SELECT * from assignment2';
$result = $conn->query($sql);
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
if(count($result)) {
echo '<table><tr>';
//Heading
foreach ($rows[0] as $columnName => $value) {
echo '<th>' . $columnName . '</th>' ;
}
echo '</tr>';
foreach ($rows as $row) {
echo '<tr>';
foreach ($row as $value) {
echo '<td>' . $value . '</td>';
}
echo '<tr>';
}
echo '</table>';
}
?>
but my question is how to make it pull out only the one that matching the login SESSION
Here is my SESSION code
<?php
session_start();
require '../ppuyakul/php/db_conn.php';
if( isset($_SESSION['user_id']) ){
$records = $conn->prepare('SELECT id,username,password FROM assignment2 WHERE id = :id');
$records->bindParam(':id', $_SESSION['user_id']);
$records->execute();
$results = $records->fetch(PDO::FETCH_ASSOC);
$user = NULL;
if( count($results) > 0){
$user = $results;
}
}
?>
Thanks so much in advance, looking forward for some nice answer ^^"
Just extend your sql query ($sql = 'SELECT * from assignment2';
) with WHERE checking if id = $_SESSION['user_id'] ($sql = 'SELECT * from assignment2 WHERE id = '.$_SESSION['user_id'];
)
//foreach
$sql = 'SELECT * from assignment2 WHERE id = '.$_SESSION['user_id'];
$result = $conn->query($sql);
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
if(count($result)) {
echo '<table><tr><th>Id</th><th>Fullname</th><th>Username</th></tr>';
foreach ($rows as $row) {
echo '<tr>';
echo '<td>'.$row["id"].'</td>';
echo '<td>'.$row["fullname"].'</td>';
echo '<td>'.$row["username"].'</td>';
echo '<tr>';
}
echo '</table>';
}