获取一组获取的字段值的计数 - MySQL / PHP
Get Count For A Set Of Fetched Field Values - MySQL / PHP
我从 MySQL 数据库返回了一些数据,该数据库输出特定用户的 post 详细信息。
我想输出图像数量的计数(在下面用 $db_image_filename
值表示)。
如何计算列中字段值的数量?我以为我可以使用 PHP 的 count()
功能,但这不起作用?
有没有办法在 PHP 中执行此操作而无需 运行 对数据库进行另一个查询(因为此数据已经从数据库中获取,我只需要它的计数值) ?然后,该值将在下面示例底部的 <p>
标记中回显。
<?php
$stmt = $connection->prepare("SELECT * FROM imageposts WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
while ($row = $stmt->fetch()) {
$db_image_id = htmlspecialchars($row['image_id']);
$db_image_title = htmlspecialchars($row['image_title']);
$db_image_tags = htmlspecialchars($row['image_tags']);
$db_image_filename = htmlspecialchars($row['filename']);
?>
<figure>
<!-- html is outputted here including values using the PHP variables above -->
</figure>
<p>Number of images: <?php // echo the count value of $db_image_filename ?></p>
<?php } ?>
在 while
循环之前和循环内部定义一个变量 $count_images = 0
,每次出现 $row['filename']
而不是 NULL
时,将变量增加 +1 或空字符串。
在 while
循环之后你可以 echo $count_images
:
<?php
$stmt = $connection->prepare("SELECT * FROM imageposts WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
$count_images = 0; // AT THE BEGINNING THERE ARE 0 IMAGES
while ($row = $stmt->fetch()) { // LOOP START
$db_image_id = htmlspecialchars($row['image_id']);
$db_image_title = htmlspecialchars($row['image_title']);
$db_image_tags = htmlspecialchars($row['image_tags']);
$db_image_filename = htmlspecialchars($row['filename']);
if ($db_image_filename != NULL && $db_image_filename != '') {
$count_images++; // IF IMAGE FOUND, INCREASE BY +1
}
?>
<figure>
<!-- html is outputted here including values using the PHP variables above -->
</figure>
<?php } // END OF WHILE LOOP ?>
<p>Number of images: <?php echo $count_images; ?></p>
我从 MySQL 数据库返回了一些数据,该数据库输出特定用户的 post 详细信息。
我想输出图像数量的计数(在下面用 $db_image_filename
值表示)。
如何计算列中字段值的数量?我以为我可以使用 PHP 的 count()
功能,但这不起作用?
有没有办法在 PHP 中执行此操作而无需 运行 对数据库进行另一个查询(因为此数据已经从数据库中获取,我只需要它的计数值) ?然后,该值将在下面示例底部的 <p>
标记中回显。
<?php
$stmt = $connection->prepare("SELECT * FROM imageposts WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
while ($row = $stmt->fetch()) {
$db_image_id = htmlspecialchars($row['image_id']);
$db_image_title = htmlspecialchars($row['image_title']);
$db_image_tags = htmlspecialchars($row['image_tags']);
$db_image_filename = htmlspecialchars($row['filename']);
?>
<figure>
<!-- html is outputted here including values using the PHP variables above -->
</figure>
<p>Number of images: <?php // echo the count value of $db_image_filename ?></p>
<?php } ?>
在 while
循环之前和循环内部定义一个变量 $count_images = 0
,每次出现 $row['filename']
而不是 NULL
时,将变量增加 +1 或空字符串。
在 while
循环之后你可以 echo $count_images
:
<?php
$stmt = $connection->prepare("SELECT * FROM imageposts WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
$count_images = 0; // AT THE BEGINNING THERE ARE 0 IMAGES
while ($row = $stmt->fetch()) { // LOOP START
$db_image_id = htmlspecialchars($row['image_id']);
$db_image_title = htmlspecialchars($row['image_title']);
$db_image_tags = htmlspecialchars($row['image_tags']);
$db_image_filename = htmlspecialchars($row['filename']);
if ($db_image_filename != NULL && $db_image_filename != '') {
$count_images++; // IF IMAGE FOUND, INCREASE BY +1
}
?>
<figure>
<!-- html is outputted here including values using the PHP variables above -->
</figure>
<?php } // END OF WHILE LOOP ?>
<p>Number of images: <?php echo $count_images; ?></p>