Question

I'm trying to output a SELECT with comma or even space but I can't design the database structure to use commas och space.

Database:

Cash Varchar 255 Decimals 0 and Default 50000 (I want it to be 50 000 or 50,000)

PHP:

$id = $_SESSION['user_id'];
$get = mysql_query('SELECT * FROM `users` WHERE `id` = '.$id.'')
<?php echo $get_row['cash']; ?>
Was it helpful?

Solution

I recommend you to change the cash field to decimal(10,2) and use number_format instead of using varchar it's not right format for money/currency value.

//  You want spaces instead of comma and dot
number_format($value, 2, ' ', ' '); // 100000.00 becomes 100 000 00

OTHER TIPS

You don't want to do that in your database. That's where you should just store the number (unformatted). Formatting is for the display script: UI, view, HTML, output etc.

In PHP, do something like this:

$value_from_database = 50000; // use $get_row['cash']
// output in view:
echo '$' . number_format($value_from_database, 2, ',', '.'); // $50,000.00 etc
echo number_format($value_from_database, 0, '', ' '); // 50 000 etc

Manual: http://php.net/number_format

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top