Jump to content
WIK

[PHP] Extragere date din mysql

Recommended Posts

Posted

bun,  ideea e ca am un tabel in baza de date care-mi stocheaza informatiile asa: 

 

{"fLeader":1,"customization":{"1":[0,0,0],"2":[0,0,0],"3":[0,0,1],"4":[0,0,1],"5":[0,0,0],"6":[0,0,0],"7":[0,0,0],"8":[0,0,1],"9":[0,0,0],"10":[0,0,1],"11":[0,0,0],"12":[0,1,0],"13":[0,0,0],"14":[0,0,255],"15":[0,1,118],"16":[0,1,255],"17":[0,0,255],"18":[16777217,0,255],"19":[1,0,255],"20":[65537,1,255],"0":[0,0,1],"p0":[-1,0],"modelhash":-265970301,"p1":[-1,0],"p10":[-1,0],"p2":[-1,0],"p3":[-1,0],"p4":[-1,0],"p5":[-1,0],"p7":[-1,0],"p6":[-1,0],"p9":[-1,0],"p8":[-1,0]},"weapons":[],"hunger":15.80829574494687,"phone_directory":[],"gaptitudes":{"physical":{"strength":30},"hacker":{"logic":0,"c++":0,"lua":0,"hacking":0},"laboratory":{"cocaine":0,"lsd":0,"weed":0},"science":{"chemicals":0,"mathematics":0}},"position":{"z":9.94322967529296,"y":-1153.960693359375,"x":-766.7114868164063},"inventory":{"wbody|WEAPON_HEAVYPISTOL":{"amount":1},"wbody|WEAPON_KNIFE":{"amount":2},"wbody|WEAPON_BAT":{"amount":1},"1":{"amount":200},"sandwich":{"amount":12},"wbody|WEAPON_SWITCHBLADE":{"amount":1},"arocola":{"amount":9},"wbody|WEAPON_PISTOL":{"amount":1},"apa":{"amount":116},"wbody|WEAPON_MACHETE":{"amount":2},"kebab":{"amount":116},"wammo|WEAPON_PISTOL":{"amount":500},"TASER":{"amount":1}},"health":188,"thirst":27.11659148989375,"groups":{"Chestor General":true,"cop":true,"Rainbow Menu":true,"recruiter":true,"ems":true,"FondatorQWWmklf":true,"user":true},"fRank":"Chestor General","fName":"Politie","cloakroom_idle":{"p10":[-1,0],"18":[33686018,2,255],"12":[0,2,0],"16":[0,2,255],"14":[0,2,255],"p6":[-1,0],"p8":[-1,0],"p0":[-1,0],"10":[0,0,2],"20":[33686018,2,255],"3":[0,0,2],"p3":[-1,0],"5":[0,0,2],"p5":[-1,0],"p7":[-1,0],"6":[0,0,2],"9":[0,1,2],"8":[0,0,2],"p1":[-1,0],"7":[0,0,2],"4":[0,0,2],"11":[0,0,2],"p9":[-1,0],"15":[0,2,228],"1":[0,0,2],"0":[0,0,2],"p4":[-1,0],"p2":[-1,0],"2":[0,0,2],"13":[0,2,0],"17":[256,2,255],"hasEquipment":true,"19":[33686018,2,255],"modelhash":1885233650}}

 

prin ce metoda as putea sa extrag datele ex:  "fRANK" : "Chestor General"  sau "fLeader":1" segmentele dintre virgule cumva...mi-am luat brain dmg sry :* ... e ceva greu ?

Posted (edited)

Depinde de versiunea de MySQL pe care o ai. Daca ai de la 5.7.8 (parca) in sus, atunci poti folosi functiile de MySQL pentru a trage direct din JSON. Daca nu, va trebui sa scoti tot campul si sa extragi din PHP ce te intereseaza.

Daca MySQL-ul tau suporta campuri JSON, poti face select-ul cu JSON_EXTRACT(denumire_camp, $.fLeader), ceva de genu'.

 

Ruleaza SELECT VERSION() ca sa vezi ce versiune ai.

 

Edit #2: Din cate imi amintesc, poti face si cu expresie regulata, insa nu iti recomand.

Edited by Wav3
Posted
49 minutes ago, SirGod said:

<?php 

$data = file_get_contents("rst.json");
$parse = json_decode($data, true);

class DataResponse extends \SplFixedArray {
	
  private $data = [];
  
  private $leader;
  private $rank;
  
  public function __construct(array $data){
  		
    $this->data = $data;
    
  }
  
  public function getLeader(){
    return $this->data["fLeader"];
  }
  
    public function getRank(){
    return $this->data["fRank"];
  }

}

$data = new DataResponse($parse);

$rank = $data->getRank();
$leader = $data->getLeader();



?>

 

 

Arata mai bine asa 

  • Downvote 1
Posted
12 hours ago, lzomedia said:

 

Arata mai bine asa 

Da, arata mai bine, e mai usor de adaptat pe viitor. Dar face acelasi lucru, doar intr-un mod mai complicat. Daca ii trebuie pentru ceva punctual, nu recomand. Daca vrea sa integreze asta intr-o aplicatie deja scrisa intr-un anume stil si la un anumit standard, e varianta mai buna, probabil. 

 

Sunt curios de un test array vs SplFixedArray, pe datele lui reale: http://johnciacia.com/2011/02/01/array-vs-splfixedarray/

Posted

tinand cont ca e doar un JSON nu are cum sa aiba sa zicem 600MB ca sa incepi sa te sperii de overhead.

eu intodeauna aleg sa merg oop chiar si pentru chestii mai simple cum e asta.

deoarece pe viitor imi va permite sa iau aceasta librarie si sa o rescriu pentru un alt proiect mult mai usor 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...