使用PHP从JSON更新MySQL表
大家好,谢谢你们的耐心 我正在尝试编写一个cron作业来更新MySQL数据库中的汇率。我收到的JSON数据如下所示使用PHP从JSON更新MySQL表,php,mysql,sql,arrays,json,Php,Mysql,Sql,Arrays,Json,大家好,谢谢你们的耐心 我正在尝试编写一个cron作业来更新MySQL数据库中的汇率。我收到的JSON数据如下所示 { "disclaimer": "Exchange rates are provided for informational purposes only, and do not constitute financial advice of any kind. Although every attempt is made to ensure quality, NO guarant
{
"disclaimer": "Exchange rates are provided for informational purposes only, and do not constitute financial advice of any kind. Although every attempt is made to ensure quality, NO guarantees are given whatsoever of accuracy, validity, availability, or fitness for any purpose - please use at your own risk. All usage is subject to your acceptance of the Terms and Conditions of Service, available at: ",
"license": "Data sourced from various providers with public-facing APIs; copyright may apply; resale is prohibited; no warranties given of any kind. Bitcoin data provided by. All usage is subject to your acceptance of the License Agreement available at:",
"timestamp": 1427914861,
"base": "CAD",
"rates": {
"AED": 2.908081,
"AFN": 45.794285,
"ALL": 103.179929,
"AMD": 373.363817,
"ANG": 1.416823,
"AOA": 85.603315,
"ARS": 6.986543,
"AUD": 1.041048,
"AWG": 1.42113,
"AZN": 0.829254,
"BAM": 1.437242,
"BBD": 1.583432,
"BDT": 61.66817,
"BGN": 1.437963,
"BHD": 0.298493,
"BIF": 1246.009421,
"BMD": 0.791716,
"BND": 1.080918,
"BOB": 5.468926,
"BRL": 2.518805,
"BSD": 0.791716,
"BTC": 0.0032649636,
"BTN": 49.501403,
"BWP": 7.855039,
"BYR": 11644.270337,
"BZD": 1.581753,
"CAD": 1,
"CDF": 733.551108,
"CHF": 0.76573,
"CLF": 0.019475,
"CLP": 490.205281,
"CNY": 4.895048,
"COP": 2038.824734,
"CRC": 422.26163,
"CUC": 0.791716,
"CUP": 0.791726,
"CVE": 80.458447,
"CZK": 20.263721,
"DJF": 140.548137,
"DKK": 5.492318,
"DOP": 35.391341,
"DZD": 77.203651
}
}
由于表已经存在,所以我只想更新表中的货币,而不是JSON文件提供给我的所有货币。我们的表中只有7种货币(它可以根据我们接受更多或更少的货币而变化)。这就是我提出的代码
<?php
define("_SITE_SECURED_FILES_",realpath(dirname(__FILE__)."/../../../../")."\Secured_Files");
// Database Credentials
require_once(_SITE_SECURED_FILES_."\Database\credentials.mysql.php");
// Open Exchange Rates Credentials
require_once(_SITE_SECURED_FILES_."\Open_Exchange_Rates\credentials.openexchangerates.php");
// Requested file
// Could also be e.g. 'currencies.json' or 'historical/2011-01-01.json'
$file = 'latest.json';
$base_currency = 'CAD';
// Open CURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://openexchangerates_org/api/{$file}?app_id={$appId}&base={$base_currency}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Get the data:
$json = curl_exec($ch);
curl_close($ch);
// Decode JSON response:
$exchangeRates = json_decode($json);
foreach($exchangeRates['rates'] as $key => $value) {
if($value) {
//how to use json array to insert data in Database
mysqli_query($mysql,"UPDATE currencies SET dblRateCAD = '".$value."' WHERE strCode = '".$exchangeRates['rates']."' LIMIT 1");
}
}
?>
您的JSON正在解码为对象,而不是数组。即使它是一个数组,您也会错误地使用它:
foreach($exchangeRates['rates'] as $key => $value) {
^^^^^^^^^^^^^^^^^^^^^^---array
mysqli_query([..snip..]WHERE strCode = '".$exchangeRates['rates']."' LIMIT 1")
^^^^^^^^^^^^^^^^^^^^^^^--same array
字符串上下文中使用的数组是文字数组
。e、 g
$foo = array();
echo "This is an $foo"; // outputs 'This is an Array'
如前所述,您很容易受到攻击。如果您想要一个数组,只需使用json\u decode($json,true)
并将查询修改为“UPDATE currences SET dblRateCAD=”“$value.”其中strCode=”$键。“”
因为json\u decode
为您提供了一个对象而不是数组。您正在接收的json包含作为对象属性的汇率,而不是作为数组。由于stdClass
对象不包含迭代器,您需要通过读取所需的每个属性来提取汇率。对于七个属性,这应该不是问题。这是一个cron job和此文件无法从网页访问,这是一个大问题吗?我想答案应该是我应该学习如何防止SQL注入,但目前,这可能是一个问题吗?谢谢。
// Decode JSON response:
$exchangeRates = json_decode($json, true);
foreach($exchangeRates['rates'] as $key => $value) {
if($value) {
//how to use json array to insert data in Database
mysqli_query($mysql,"UPDATE currencies SET dblRateCAD = '".$value."' WHERE strCode = '". $key ."' LIMIT 1");
}
}