如何使用php选择2行的整列数据?

时间:2015-06-06 08:41:29

标签: php html mysql

让我解释一下我想要做什么。我将用户的信息存储在名为“students_data”的mysql表中。在这里,现在我只想在下表中打印这些名称,但登录的用户除外。假设我总共有5个用户列表包括我在桌面上的名字,姓氏和其他详细信息。 在表格中的h2标签下方,我想打印桌面上只有名字和姓氏的名字,显然不包括我。

<?php
$serverName="localhost";  
$userName="samsung";  
$password="8198982039";  
$dataBase="students_record";  
$conn=mysqli_connect($serverName,$userName,$password,$dataBase);  
$sql= "SELECT First_Name FROM students_data";  
$result=mysqli_query($conn,$sql);  
$cols=mysqli_fetch_array($result,MYSQLI_BOTH);  
?>  

<html>  
<head>  
    <title></title>  
    <link rel="stylesheet" type="text/css" href="bootstrap.min.css">  
    <meta name="viewport" content="width=device-width,initial-scale=1" >  
    <style>  
    </style>  
</head>  
<body>
<div class="container">  
<div class="col-sm-4"></div>  
<div class="col-sm-4" >  
    <h2>Friends You may know</h2>  
    <table class="table table-striped">  
        <tr>  
            <td><?php?></td>  
        </tr>  
    </table>  
</div>  
<div class="col-sm-4" >  
    <h2>Your status</h2>  
</div>  
</div>  
</body>  
</html>  

2 个答案:

答案 0 :(得分:0)

如果您正在使用会话

$idtoexclude = $_SESSION['onlineuserid'];
$sql= "SELECT * FROM students_data WHERE NOT id = '$idtoexclude'";

答案 1 :(得分:0)

请相应调整变量,它将适合您。感谢: -

<?php
$serverName="localhost";  
$userName="samsung";  
$password="8198982039";  
$dataBase="students_record";  
$logged_in_user_id = $_SESSION['user_id']; // i assume that you store logged-in user in in session variable having name user_id
$conn=mysqli_connect($serverName,$userName,$password,$dataBase);  
$sql= "SELECT First_Name,Last_name FROM students_data where id !='".$logged_in_user_id.""; // get all user except logged in user
$result=mysqli_query($conn,$sql);  

$user_data = array();
$i = 0;
while($cols=mysqli_fetch_assoc($result,MYSQLI_BOTH);) {
    $user_data[$i]['first_name'] = $cols['First_Name'];// get first name
    $user_data[$i]['last_name'] = $cols['Last_Name'];   // get last name
}
?>  

<html>  
<head>  
    <title></title>  
    <link rel="stylesheet" type="text/css" href="bootstrap.min.css">  
    <meta name="viewport" content="width=device-width,initial-scale=1" >  
    <style>  
    </style>  
</head>  
<body>
<div class="container">  
<div class="col-sm-4"></div>  
<div class="col-sm-4" >  
    <h2>Friends You may know</h2>  
    <table class="table table-striped">
    <?php foreach ($user_data as $user){//iterate that user data array?> 
        <tr>  
            <td><?php echo $user['first_name'];?></td>  
            <td><?php echo $user['last_name']?></td>  
        </tr>
    <?php } ?>      
    </table>  
</div>  
<div class="col-sm-4" >  
    <h2>Your status</h2>  
</div>  
</div>  
</body>  
</html>