How to: Format a MySQL query result with currency signs using PHP


Problem :

I have a MySQL query that fetches a numeric value VARCHAR (20) from a table, and I need to format the result to include thousands separators (,) and decimal separators (.) to two positions.

I've searched a lot for a simple query, so now I want to know if this is possible using CSS.

Here is the table data:

Income
-------------------
1234456789
234456000
987456000

And so on. Here's the query:

// Fetch data

$sql="SELECT * FROM scoreboard WHERE codcliente = '".$q."' OR nombre LIKE '%".$q."%'";
$result = mysql_query($sql);
if(mysql_num_rows($result) == 0){
  echo '<div align="center" style="background-color:#CCCCCC; font-weight:bold; color:#C0504D;">Record no existe.&nbsp;&nbsp;&nbsp;<input id="srchcreate" type="button" value="Crear" /></div>';
  return;
  }
//echo $result;

And the result:

// Construct the table

echo "<table>";

// Construct the array

while($row = mysql_fetch_array($result)){
  echo "<tr>";
  echo "<td>" . $row['codcliente'] . "</td>";
  echo "<td>" . $row['nombre'] . "</td>";
//  echo "<td>" . $row['ejecutivo'] . "</td>";
//  echo "<td>" . $row['banca_as400'] . "</td>";
//  echo "<td>" . $row['banca_real'] . "</td>";
//  echo "<td>" . $row['ingresos'] . "</td>";
  echo "<td>" . $row['ciiu'] . "</td>";
//  echo "<td>" . $row['division'] . "</td>";
  echo "<td>" . $row['actividad'] . "</td>";
  echo "<td>" . $row['riesgo_industria'] . "</td>";
  echo "<td>" . $row['riesgo_cliente'] . "</td>";
  echo "<td>" . $row['fecha'] . "</td>";
  echo "<td>" . $row['analista'] . "</td>";
  echo "</tr>";
}
echo "</table>";

This gets inserted to a PHP file and displayed as a table.



Solution :

<?php
echo number_format(123456789, 2);
?>

123,456,789.00

echo "<td>".number_format($row['ingresos'], 2)."</td>\n"

Mysql Tutorials

Mysql Howto..

How to store and search efficiently huge MySQL lottery database

how to synchronize two databases of different servers?

How to sort by name in alphabets using mysql query

How to generate crystal report using mysql database in asp.net 2010

How should MySQL be installed in a Django application?

How do you get the last access (and/or write) time of a MySQL database?

How to get all data of a open api site

How to Sync MySQL with python?

How to import mysql dump file to MySQL server using java on windows (7)

How to set Rank function in hypersql database(HSQLDB)

How to properly use mysql MONTH() and YEAR() in codeigniter active record

how to retrieve and output images from multiple columns with php and mysql?

How to install MySQL on Mavericks/Mac (10.9.2) correctly?

How to use mysql IN() function in php?

How to optimize mysql queries and decrease server load?

How to “sync”/revision-control an entire mysql database with all git commits and checkouts?

How can I optimize or combine these MySQL queries?

How do I setup a GUI to MySql? [duplicate]

How can I set up a mysqli object without actually connecting yet?

How to Carry forward a column value while inserting other column values in mysql?

How to create summary of data with the ID the data would insert with

How to Update Set using MySQL

How to abbreviate MySQL queries in PHP?

How to lock rows in a queue?

How to connect these 3 programming languages?

How to generate count query for a group by select query in MySql

MySQL how to do COUNT(*) on a query with joins?

How to use NHibernate with both MySQL server and Microsoft SQL server 2008

How to generate 1000000 rows with random data?

Set WinForm Show Date from MySQL