» »

PHP - XML Parser -> baza

PHP - XML Parser -> baza

Klemen1602 ::

Zdravo!

Malo se ukvarjam z parsanjem xml podatkov, prišel sem do tega da jih preberem in izpljunem v HTML obliki, sedaj pa bi rad te podatke shranil v bazo, da jih lahko kasneje na hitrejši način prikažem na spletni strani.

Dosedanje prefiltrirano delo:
<?php

$xml = simplexml_load_file('http://cachepricefeeds.williamhill.com/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=46&marketSort=MR&filterBIR=N');

foreach ($xml->response->williamhill->class->type as $type) {
  $type_attrib = $type->attributes();
  $type_attrib['id'];
  $type_attrib['name'];
  foreach ($type->market as $event) {
    $event_attrib = $event->attributes();
    $event_attrib['id'];
    $event_attrib['name'];
    $event_attrib['date'];
    $event_attrib['url'];
    foreach ($event->participant as $participant) {
        $participant_attrib = $participant->attributes();
          $participant_attrib['name'];
          $participant_attrib['oddsDecimal'];
    }
  }
}

?>


Vsak "attrib" sem poskušal dati v array pa se mi zdi da to ni najbolj praktična rešitev (gledano na rezultate :D), mogoče pa je to prava rešitev in sem jo poskušal realizirati na preveč laičen način ... ?

Hvala za pomoč!
Klemen

Klemen1602 ::

Poizkusil sem z naslednjo kodo:

<?php

//mysql connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("bet_sql") or die(mysql_error());

$xml = simplexml_load_file('http://cachepricefeeds.williamhill.com/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=46&marketSort=MR&filterBIR=N');

foreach ($xml->response->williamhill->class->type as $type) {
  $type_attrib = $type->attributes();
  $type_attrib['id'];
  $type_attrib['name'];
  foreach ($type->market as $event) {
    $event_attrib = $event->attributes();
    $event_attrib['id'];
    $event_attrib['name'];
    $event_attrib['date'];
    $event_attrib['url'];
    foreach ($event->participant as $participant) {
        $participant_attrib = $participant->attributes();
          $participant_attrib['name'];
          $participant_attrib['oddsDecimal'];

          mysql_query("INSERT INTO games (type_id, type_name, event_id, event_name, event_url, participant_name, participant_odds) 
          VALUES ($type_attrib[id], $type_attrib[name], $event_attrib[id], $event_attrib[name], $event_attrib[url], $participant_attrib[name], $participant_attrib[oddsDecimal]) ")
          or die(mysql_error());
    }
  }
}

?>


Na zalost mi vrže ven napako:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Jupiler League, 134465283, Standard Liege v Oostende - Match Betting, http://spo' at line 2


Any ideas?

Ziga Dolhar ::

Se mi samo zdi, al res vrednosti nikoli ne escape-aš, ne-numeričnih tipov podatkov pa ne daš med narekovaje?

(Tvoja koda je sicer izjemno varnostno vprašljiva.)
https://dolhar.si/

Zgodovina sprememb…



Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

SQL vprasanje (strani: 1 2 )

Oddelek: Programiranje
687886 (4565) BivšiUser2
»

[PHP]Zajem podatkov iz baze

Oddelek: Programiranje
353779 (3416) cobrica
»

[MySql]Sintaksa poizvedbe

Oddelek: Programiranje
61465 (1386) KernelPanic
»

MySQL BETWEEN dates?

Oddelek: Programiranje
111289 (1203) Housy
»

sql injection [PHP + mysql]

Oddelek: Informacijska varnost
111757 (1482) Lion29

Več podobnih tem