Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting a mysql number from a select statement into php

I am using mysql 5.1 database and have a # of fields in them that look like this for format:

1234567

When I output them to via php, I would like them formatted like this: 1,234,567

There are no decimals involved.

The select statement is pulling 30 records from my database. Each record has (2) fields that I need formatted thus. Weight and Cost as well as 16 other fields that I need, but do not need formatting. They are being read and entered int a table.

Suggestions?

Note: Data Fields: Cost Weight
Table: Warships
Current Select statement:

$result = mysql_query('SELECT * FROM `Warships` WHERE `Type` = "BC" ');
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['ID'] . "</td>";
  echo "<td>" . $row['SHIP CLASS'] . "</td>";
  echo "<td>" . $row['CAT'] . "</td>";
  echo "<td>" . $row['Type'] . "</td>";
  echo "<td>" . $row['WEIGHT'] . "</td>";
  echo "<td>" . $row['Cost'] . "</td>";
like image 792
Larry Avatar asked Mar 23 '26 21:03

Larry


1 Answers

I think PHP's number_format is what you need.

$num = number_format(1234567);
echo $num; // 1,234,567

If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands. More info from PHP API

like image 83
Marc Avatar answered Mar 26 '26 09:03

Marc