1

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 ^^"

6
  • 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'];)
    – Autista_z
    May 18, 2017 at 7:46
  • What exactly ain't working? Your second code preview does seem to filter the assignment2 table for only that of the session. May 18, 2017 at 7:46
  • @Autista_z Thanks for your reply i will try it out and let u know the result. May 18, 2017 at 7:59
  • @Cpt.Kangar00 it will print out entire database table from assignment2 =( May 18, 2017 at 7:59
  • @Autista_z Its' WORKING !!!! by using $sql = 'SELECT * from assignment2 WHERE id = '.$_SESSION['user_id']; Thanks so much for you help May 18, 2017 at 8:06

1 Answer 1

0

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>';
    }
8
  • Thanks a lot for your help, it work like a charm ^^" May 18, 2017 at 8:39
  • Thanks a lot for your help, it work like a charm ^^" But i have one more quick question wanna ask i try this code but i come up with error Notice: Undefined index: ID in Notice: Undefined index: fullname in Notice: Undefined index: username in <?php $sql = 'SELECT * from assignment2 WHERE id = '.$_SESSION['user_id']; $result = $conn->query($sql); while ($rows = $result->fetchAll(PDO::FETCH_ASSOC)) { echo "<tr>"; echo "<td>".$rows['ID']."</td>"; echo "<td>".$rows['fullname']."</td>"; echo "<td>".$rows['username']."</td>"; echo "</tr>"; } ?> May 18, 2017 at 8:43
  • Do you have in your table assigment2 column with name fullname?
    – Autista_z
    May 18, 2017 at 8:45
  • yup, I have that in my database, May 18, 2017 at 8:54
  • Ah. You cant make while like it. $rows = $result->fetchAll(PDO::FETCH_ASSOC) returns array of results, no concrete result, so you cant use inside while just $rows['id']. Use foreach like you was using it above.
    – Autista_z
    May 18, 2017 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.