douyuan4697
2013-08-29 08:24
浏览 45
已采纳

PHP显示来自mysql的记录

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

图片转代码服务由CSDN问答提供 功能建议

我在以下部分代码中被阻止...

我有< strong> config.php 页面,其中包含以下代码:

 &lt;?php 
 //启动会话(非常重要!)
session_start()  ; 
 
 //建立数据库链接
 $ dbLink = mysql_connect('localhost','USER','PASS'); 
if(!$ dbLink)die('无法建立连接 到数据库:'。mysql_error()); 
 
 $ dbSelected = mysql_select_db('DATABASE',$ dbLink); 
if(!$ dbSelected)die('我们连接,但不能使用 表:'。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');  
退出;  
} 
} 
?&gt; 
   
 
 

现在我有了另一个页面,其中包含以下代码:

  &lt;?php include_once('config.php');  ?&gt; 
 
&lt;?php foreach($ _ SESSION ['user'] as $ key =&gt; $ value){?&gt; 
&lt; li&gt;&lt;?php echo $ key;  ?&GT;  &lt; strong&gt;&lt;?php echo $ value;  ?&gt;&lt; / strong&gt;&lt; / li&gt; 
&lt;?php}?&gt; 
   
 
 

该代码显示存储在数据库中的所有信息,一个 一个..

我想在我的页面中单独显示信息,例如:

 &lt;?php echo $ email  ;  ?&gt; 
   
 
 

etcetera ..

有人能解释我怎么做吗? \ n

谢谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dpl9717 2013-08-29 08:29
    已采纳

    You should output it like

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

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

    点赞 评论
  • dou7466 2013-08-29 08:31

    Just address the relevant field in the associative array:

      <?php echo $value['email';?>
    
    点赞 评论
  • dongli2000 2013-08-29 08:48

    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.

    点赞 评论

相关推荐 更多相似问题