1

Im blocked at following part of code...

i have config.php page, with following code inside:

<?php
// Start the session (pretty important!)
session_start();

// Establish a link to the database
$dbLink = mysql_connect('localhost', 'USER', 'PASS');
if (!$dbLink) die('Can\'t establish a connection to the database: ' . mysql_error());

$dbSelected = mysql_select_db('DATABASE', $dbLink);
if (!$dbSelected) die ('We\'re connected, but can\'t use the table: ' . mysql_error());

$isUserLoggedIn = false;  
$query      = 'SELECT * FROM users WHERE session_id = "' . session_id() . '" LIMIT 1';  
$userResult     = mysql_query($query);  
if(mysql_num_rows($userResult) == 1){  
$_SESSION['user'] = mysql_fetch_assoc($userResult);  
$isUserLoggedIn = true;  
}else{  
if(basename($_SERVER['PHP_SELF']) != 'index.php'){  
    header('Location: index.php');  
    exit;  
}  
}  
?>

Now i have another page, with following code inside:

<?php include_once('config.php'); ?>

<?php foreach($_SESSION['user'] as $key => $value){ ?>
<li><?php echo $key; ?> <strong><?php echo $value; ?></strong></li>
<?php } ?>

That code show me all informations stored in database, one by one..

I want to show informations, individualy in my page, something like:

<?php echo $email; ?>

etcetera..

Can someone explain me how to do that?

Thank you

1
  • why not remove some boiler plate from the code above and try to debug the above with something like PHPStorm? Your question would amount to "how do I query a database from php" and google has plenty of those Commented Aug 29, 2013 at 8:31

3 Answers 3

2

You should output it like

<?php echo $_SESSION['user']['email'] ?>

assuming that 'email' is the column name in the users table.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much my friend.. it's exactly what i want.. Ty !
2

Just address the relevant field in the associative array:

  <?php echo $value['email';?>

Comments

1

Use this in the config file:

$_SESSION['user']['email'] = mysql_fetch_assoc($userResult); 

instead of this

$_SESSION['user'] = mysql_fetch_assoc($userResult); 

in your code. You can pass as many columns you want to pass from your table.

And in another page use

foreach($_SESSION['user']['email'] as $key => $value) 

instead of

foreach($_SESSION['user'] as $key => $value) 

in your code.

Remember number of columns you pass in config file, all those columns should be called in this page.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.