php sql 格式化 SELECT 列编号

php sql formatting SELECT columns numbers

我正在从 SQL table 中检索各种列,其中一些是数字或货币,在示例中我们说:

$sql="SELECT id_column, event_column, amount_column FROM table1";

然后我用那个给他们看:

$result = mysqli_query($conn,$sql);

echo "<table border='1'>
        <tr>
          <th>id</th>
          <th>event time</th>
          <th>amount</th>
        </tr>";

while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
      echo "<td>" . $row[' id_column '] . "</td>";
      echo "<td>" . $row[' event_column '] . "</td>";
      echo "<td>" . $row[' amount_column '] . "</td>";
    echo "</tr>";
}

echo "</table>";

是否可以更改 amount_column 中数字的格式?

我看到应该可以使用命令以我喜欢的方式更改单个数字数据 - number_format($number, 2, ',', '.') – 但这似乎不是申请整列

我需要的是在一 (yy) 下使用逗号表示小数,其他的以 3 (x.xxx) 千为单位使用点,例如 xx.xxx.xxx,yy

有人有什么建议吗? (包括如何在我通过表单输入数据时更改 PHP 或 SQL 中的设置,这些数据使用逗号而不是小数点,但 SQL 以不同的方式保存它们– 我想 UK/USA 十进制标点符号,而我需要欧盟 Italian/Germany 标点符号,或者至少是 ISO 标准,使用逗号表示十进制,space 表示每组三个数字)。

更新:

是的,正确的 money_format 不适用于 windows。谢谢戴夫。 :)

使用number_format( $row[' amount_column '], 2, ',', '.')

它给你类似 123.456.789,12

PHP number_format


(不在 windows!)

您可以使用 php 的 money_format('%i', $row[' amount_column '])

PHP money_format

<?php

setlocale(LC_MONETARY, 'en_US');


$result = mysqli_query($conn,$sql);

echo "<table border='1'>
        <tr>
          <th>id</th>
          <th>event time</th>
          <th>amount</th>
        </tr>";

while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
      echo "<td>" . $row[' id_column '] . "</td>";
      echo "<td>" . $row[' event_column '] . "</td>";
      echo "<td>" . money_format('%i', $row[' amount_column ']) . "</td>";
    echo "</tr>";
}

echo "</table>";

正确答案是我的评论:

echo "<td>" . number_format($row['amount_column'], 2, ',', ' ') . "</td>";

你不能做:

$row[' amount_column ']

数组索引计数的间距!

这里有证明https://ideone.com/FtPEc6

所以从技术上讲,您认可的答案是错误的。