SQL INSERT中的数字格式

时间:2013-06-15 11:28:30

标签: php mysql sql json

大家好我需要在SQL INSERT INTO

中对数字格式提供一些支持

我需要将{$ data ['rank'] ['points']}转换为数字格式,以便将其显示为5467389,而不是将其转换为54,673,89 数据正在从API数据中导入

这是我的代码

$host="hostname"; // Host name
$username="database"; // Mysql username
$password="password"; // Mysql password
$db_name="dbname"; // Database name
$tbl_name="table"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


// API Data
$url = "http://api.erpk.org/citizen/profile/3121752.json?key=Yn3AsG80";
$json = $json = file_get_contents($url);
$data = json_decode($json, true);

date_default_timezone_set('Europe/London');

// Insert data into mysql
$sql="INSERT INTO $tbl_name(citid, citname, rankpoints)VALUES('{$data['id']}', '{$data['name']}', '{$data['rank']['points']}')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='supplies_insert.php'>Back to main page</a>";
}

else {
echo "ERROR";
}

2 个答案:

答案 0 :(得分:1)

试试这个:

$query = sprintf("INSERT INTO `%s` (citid, citname, rankpoints) VALUES ('%s', '%s', '%s')",
  mysql_real_escape_string($tbl_name),
  $data['id'],
  mysql_real_escape_string($data['name']}),
  mysql_real_escape_string(number_format($data['rank']['points'])));
$result = mysql_query($query);

答案 1 :(得分:0)

如果我正确理解您的要求,您可以使用php的number_format()函数。如果那不是您想要的,请告诉我。