PHP/MySQL - inserting JSON data into DB on Bluehost server, empty DB entry -


this one-use script insert json data mysql database on bluehost. i've used various echo statements inside , outside loop make sure json info parsed correctly , loop works expected. bluehost files tell me use sql statements instead of sqli or dbo.

<?php     $con = mysql_connect ("localhost:port", "username", "password");     if (!$con) {         die('could not reach database: error code ' . mysql_error() . "<br>");     } else {         echo 'connected database. ' . "<br>";     }     mysql_select_db ("db_name", $con);      $jsondata = file_get_contents('bfz.json');     $data = json_decode($jsondata, true);      $cards = $data['cards'];      // loop through cards array , load each set of variables db     for($i = 0; $i <= count($cards); $i++) {         $card_name = $cards[$i]['name'];         $card_manacost = $cards[$i]['manacost'];         $card_cmc = $cards[$i]['cmc'];          // clear variable last use         $card_colors = "";          // if card has no color info, assign text "colorless"         if ($cards[$i]['colors'] == "") {             $card_colors = "colorless";         // else if card has color info, convert colors array 1 long text variable         } else {             for($colorindex = 0; $colorindex < count($cards[$i]['colors']); $colorindex++) {                 $card_colors = $card_colors . $cards[$i]['colors'][$colorindex] . " ";             }         }          // various bits load db         $card_type = $cards[$i]['type'];         $card_rarity = $cards[$i]['rarity'];         $card_text = $cards[$i]['text'];         $card_number = $cards[$i]['number'];         $card_power = $cards[$i]['power'];         $card_toughness = $cards[$i]['toughness'];         $card_multid = $cards[$i]['multiverseid'];         $card_id = $cards[$i]['id'];          // insert data cards table         $sql = "insert cards (card_id, card_name, manacost, cmc, colors, type, rarity, card_text, card_number, power, toughness, multiverseid)         values ('$card_id', '$card_name', '$card_manacost', '$card_cmc', '$card_colors', '$card_type', '$card_rarity', '$card_text', '$card_number', '$card_power', '$card_toughness', '$card_multid')";     }     if (!mysql_query($sql, $con)) {         die('error: ' . mysql_error());     } else {         echo "data inserted correctly. <br>";     }     $con->close(); ?> 

the last "data inserted correctly" triggers, db has 1 entry 0 stored in cmc, power, toughness, , multiverseid , colorless in colors entry. card_id primary key , it's blank.

either db isn't set or i'm missing in code. i'd include screenshot of db structure i'm not sure if that's allowed here. collation set utf8_general_ci , varchar on text entries , int or smallint numbers. json structure here: http://mtgjson.com/ .

i didn't know php , mysql until 2 days ago forgive me if i'm missing simple. other questions i've read don't seem address issue.

your mysql_query outside for-loop, thats why executed once. should be

for($i = 0; $i <= count($cards); $i++) {     //     //...     // insert data cards table     $sql = "insert cards (card_id, card_name, manacost, cmc, colors, type, rarity, card_text, card_number, power, toughness, multiverseid)        values ('$card_id', '$card_name', '$card_manacost', '$card_cmc', '$card_colors', '$card_type', '$card_rarity', '$card_text', '$card_number', '$card_power', '$card_toughness', '$card_multid')";      //----> here instead     if (!mysql_query($sql, $con)) {        die('error: ' . mysql_error());     } else {        echo "data inserted correctly. <br>";     } } 

$card_type = mysql_real_escape_string($cards[$i]['type']); $card_rarity = mysql_real_escape_string($cards[$i]['rarity']); 

etc


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -