gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:21:22 25 4
gpt4 key购买 nike

让我解释一下我实际上想要做什么。我将用户信息存储在名为“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>

最佳答案

请相应地调整变量,它会对您有用。谢谢:-

<?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>

关于php - 如何使用php选择2行的整列数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30680766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com