Arian
Arian

Reputation: 11

Using & Filtering Data with PHP

I'm coding a simple website and currently I'm working on the User System. I coded a Login & Register and want to show the saved data to the specific user. Example: I get an ID when I log-in, now I want to receive all the data that is saved in the DB on my own ID. I don't have an idea how to do that and appreciate every little help. I'm able to show the User ID because it's saved in the Sessions, but no other data.

Everything I got at the moment, is a script that shows me the whole DB.

<?php 
$db = new PDO( 'mysql:host=localhost;dbname=betaddicted', 'root', '' );
$sql = "SELECT * FROM users";
$query = $db->prepare( $sql );
$query->execute();
$results = $query->fetchAll( PDO::FETCH_ASSOC );

foreach( $results as $row ){
  print_r( $row );
}

Thanks Guys!

Upvotes: 1

Views: 49

Answers (1)

Masivuye Cokile
Masivuye Cokile

Reputation: 4772

<?php 
$db = new PDO( 'mysql:host=localhost;dbname=betaddicted', 'root', '' );
$sql = "SELECT * FROM users where id = ?";
$query = $db->prepare( $sql );
$query->bindValue(1,$_SESSION['id']);
$query->execute();
$results = $query->fetchAll( PDO::FETCH_ASSOC );

foreach( $results as $row ){
  print_r($row );
}

Upvotes: 1

Related Questions