gpt4 book ai didi

php - 无法检索每条记录的额外查询的值?

转载 作者:行者123 更新时间:2023-11-29 08:24:31 26 4
gpt4 key购买 nike

我正在处理一个表,将从数据库中选取用户的数据。

问题是我无法在 while 条件上放置两个语句,因为我从不同的表和行获取数据。

@代码

<?php
$con = mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database_name") or die(mysql_error());

$result = mysql_query("select * FROM users");

$space_used = mysql_query("SELECT SUM(fileSize) FROM file WHERE file.userId=users.id AND file.statusId=1");

$total_files = mysql_query("SELECT COUNT(id) FROM file WHERE file.userId=users.id AND file.statusId=1");

echo "<table class='table table-striped table-hover table-bordered' id='sample_editable_1'>
<tr>
<th>Username</th>
<th>Type</th>
<th>Email</th>
<th>Last Login</th>
<th>Last IP</th>
<th>space_used</th>
<th>total_files</th>
<th>status</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['level'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['lastlogindate'] . "</td>";
echo "<td>" . $row['lastloginip'] . "</td>";
echo "<td>" . $space_used['space_used'] . "</td>";
echo "<td>" . $total_files['total_files'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "</tr>";
}

echo "</table>";
mysql_close($con);
?>

我得到的是用户名、级别、电子邮件、laSTLogindate、laSTLoginip 和状态的所有值。我的表格上没有显示的两个是我要实现的第二个条件:space_used 和total_files。

我做错了什么?我不是 php 专家,事实上,我混合了教程和脚本来达到这个结果,这有点复杂。

谢谢

最佳答案

您需要重新处理您的查询,您可以使用 JOIN 将 3 个查询恢复为单个查询:

  SELECT u.*, 
SUM(f.fileSize) AS space_used,
COUNT(f.id) AS total_files
FROM users u
JOIN file f
ON f.userId = u.id AND f.statusId = 1
GROUP BY u.id

然后你可以这样阅读:

<?php
// your database info
$db_host = 'your MySQL server host';
$db_user = 'your username';
$db_pass = 'your password';
$db_name = 'your database name';
$con = new mysqli($db_host, $db_user, $db_pass, $db_name);
if($con->connect_error)
{
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}

if (!$result = $con->query("SELECT u.*,
SUM(f.fileSize) AS space_used,
COUNT(f.id) AS total_files
FROM users u
JOIN file f
ON f.userId = u.id AND f.statusId = 1
GROUP BY u.id"))
{
die('Select query error: ' . $con->error);
}
?>
<table class='table table-striped table-hover table-bordered' id='sample_editable_1'>
<tr>
<th>Username</th>
<th>Type</th>
<th>Email</th>
<th>Last Login</th>
<th>Last IP</th>
<th>space_used</th>
<th>total_files</th>
<th>status</th>
</tr>
<?php
while ($row = $result->fetch_array())
{
?>
<tr>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['level']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['lastlogindate']; ?></td>
<td><?php echo $row['lastloginip']; ?></td>
<td><?php echo $row['space_used']; ?></td>
<td><?php echo $row['total_files']; ?></td>
<td><?php echo $row['status']; ?></td>
</tr>
<?php
}
$con->close();
?>
</table>

关于php - 无法检索每条记录的额外查询的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18323801/

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