-1

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']; ?>
4

2 回答 2

2

你不想在你的数据库中这样做。那是您应该只存储数字(未格式化)的地方。格式化用于显示脚本:UI、视图、HTML、输出等。

在 PHP 中,执行以下操作:

$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

手册: http: //php.net/number_format

于 2014-02-12T01:03:43.807 回答
2

我建议您将该cash字段更改为decimal(10,2)并使用number_format,而不是使用varchar它不是货币/货币价值的正确格式。

//  You want spaces instead of comma and dot
number_format($value, 2, ' ', ' '); // 100000.00 becomes 100 000 00
于 2014-02-12T01:11:16.200 回答