how to convert array to mysql in php -
does know how can pull currency (isoalphacode
) , sellnote
following array , add mysql database?
array ( [wrappedobject] => array ( [0] => array ( [basecurrency] => array ( [id] => 826 [description] => great british pound [isoalphacode] => gbp ) [fxcurrency] => array ( [id] => 978 [description] => euro [isoalphacode] => eur ) [buynote] => 6.1 [sellnote] => 1.1495 [buycheque] => 9.6 [sellcheque] => [rank] => high [denominations] => array ( [0] => 20 [1] => 50 [2] => 100 [3] => 200 ) [degradation] => 5 [upsellingdenomination] => 20 [collectionorderdenominations] => [isexotic] => ) [1] => array ( [basecurrency] => array ( [id] => 826 [description] => great british pound [isoalphacode] => gbp ) [fxcurrency] => array ( [id] => 840 [description] => dollar [isoalphacode] => usd ) [buynote] => 6 [sellnote] => 1.2268 [buycheque] => 9.6 [sellcheque] => [rank] => high [denominations] => array ( [0] => 10 [1] => 20 [2] => 50 [3] => 100 ) [degradation] => 1 [upsellingdenomination] => 20 [collectionorderdenominations] => [isexotic] => ) [2] => array ( [basecurrency] => array ( [id] => 826 [description] => great british pound [isoalphacode] => gbp ) [fxcurrency] => array ( [id] => 36 [description] => australian dollar [isoalphacode] => aud ) [buynote] => 5.95 [sellnote] => 1.6201 [buycheque] => 5.95 [sellcheque] => [rank] => low [denominations] => array ( [0] => 20 [1] => 50 ) [degradation] => 5 [upsellingdenomination] => 20 [collectionorderdenominations] => [isexotic] => 1 ) [3] => array ( [basecurrency] => array ( [id] => 826 [description] => great british pound [isoalphacode] => gbp ) [fxcurrency] => array ( [id] => 48 [description] => bahraini dinar [isoalphacode] => bhd ) [buynote] => 8.7 [sellnote] => 0.4456 [buycheque] => [sellcheque] => [rank] => low [denominations] => array ( [0] => 10 [1] => 20 ) [degradation] => 1 [upsellingdenomination] => 1 [collectionorderdenominations] => [isexotic] => 1 ) [4] => array ( [basecurrency] => array ( [id] => 826 [description] => great british pound [isoalphacode] => gbp ) [fxcurrency] => array ( [id] => 52 [description] => barbados dollar [isoalphacode] => bbd ) [buynote] => 9.7 [sellnote] => 2.324 [buycheque] => [sellcheque] => [rank] => low [denominations] => array ( [0] => 20 [1] => 50 [2] => 100 ) [degradation] => 2 [upsellingdenomination] => 2 [collectionorderdenominations] => [isexotic] => 1 ) ) [valid] => 1 [errormessage] => [errorcauses] => )
$arr = json_decode('{"wrappedobject":[{"basecurrency":{"id":826,"description":"great british pound","isoalphacode":"gbp"}...', true); echo $arr["wrappedobject"][0]["basecurrency"]["isoalphacode"]; // outputs "gbp"
use bit of code.
it uses json_decode
method have been using already. make sure set second parameter true
.
store json array rather object.
update
following @user2401723 comment below.
i don't understand storing in json, seems take user's base currency (i assume currency of country) , stored "basecurrency"
, repeated in each property in object.
consider line of code.
echo $arr["wrappedobject"][0]["basecurrency"]["isoalphacode"]; // outputs "gbp" echo $arr["wrappedobject"][0]["sellnote"]; // outputs 1.1495
this outputs user's base currency.
since [0]
euro (obtained writing $arr["wrappedobject"][0]["fxcurrency"]["isoalphacode"]
), assume sellnote
exchange rate between basecurrency
, fxcurrency
.
i don't know how want use data write simple loop can see isoalphacode
s , sellnote
s.
// first line display base currency echo $arr["wrappedobject"][0]["basecurrency"]["isoalphacode"]; // loop display every currency code , exchange rate (if that's sellnote is) ($i = 0; $i < count($arr["wrappedobject"]); $i++) { echo $arr["wrappedobject"][$i]["fxcurrency"]["isoalphacode"]; echo $arr["wrappedobject"][$i]["sellnote"]; }
mysqli
regards inserting mysql, don't know whether using pdo or mysqli give example of mysqli.
$dbhost = "localhost"; $dbuser = "root"; $dbpass = ""; $dbname = "mydbname"; $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname); ($i = 0; $i < count($arr["wrappedobject"]); $i++) { $isoalphacode = $arr["wrappedobject"][$i]["fxcurrency"]["isoalphacode"]; $sellnote = $arr["wrappedobject"][$i]["sellnote"]; $query = $mysqli->prepare("insert mytable(isoalphacode, sellnote) values(?, ?)"); $query->bind_param("sd", $isoalphacode, $sellnote); $query->execute(); } $query->close(); $mysqli->close();
line line:
$dbhost
- host of database.$dbuser
- username of database.$dbpass
- password database.$dbname
- name of database.- newline.
$mysqli = new mysqli(...)
- creating mysqli object. see documentation. store our connection database.- newline.
for ($i = 0; $i < count($arr["wrappedobject"]); $i++)
- since of currencies stored under$arr["wrappedobject"]
access index ,count
determine length.- opening code block.
$isoalphacode
- store currencyisoalphacode
in variable. referencing$i
index in array.$sellnote
- float exchange rate .- newline.
$mysqli->prepare("... values(?, ?")
- prepared statement.
extremely important use prepared statements since prevent sql injections.
?
tells query going insert data here.bind_param("sd", $isoalphacode, $sellnote)
- add our parameters query.
"sd"
- tells query our first?
string
, seconddouble
. list our parameters after in order relates?
.$query->execute()
- execute query.- closing code block.
$query->close()
- close query$mysqli->close()
- close connection (only once finished it).
Comments
Post a Comment