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 isoalphacodes , sellnotes.

// 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:

  1. $dbhost - host of database.
  2. $dbuser - username of database.
  3. $dbpass - password database.
  4. $dbname - name of database.
  5. newline.
  6. $mysqli = new mysqli(...) - creating mysqli object. see documentation. store our connection database.
  7. newline.
  8. for ($i = 0; $i < count($arr["wrappedobject"]); $i++) - since of currencies stored under $arr["wrappedobject"] access index , count determine length.
  9. opening code block.
  10. $isoalphacode - store currency isoalphacode in variable. referencing $i index in array.
  11. $sellnote - float exchange rate .
  12. newline.
  13. $mysqli->prepare("... values(?, ?") - prepared statement.
    extremely important use prepared statements since prevent sql injections.
    ? tells query going insert data here.
  14. bind_param("sd", $isoalphacode, $sellnote) - add our parameters query.
    "sd" - tells query our first ? string , second double. list our parameters after in order relates ?.
  15. $query->execute() - execute query.
  16. closing code block.
  17. $query->close() - close query
  18. $mysqli->close() - close connection (only once finished it).

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -