Guest User

Untitled

a guest
Mar 9th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. <?php
  2. class User {
  3.  
  4. public function Connect()
  5. {
  6. $dsn = "mysql:host=localhost;dbname=auto;charset=utf8";
  7. $opt = array (
  8. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  9. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
  10. );
  11. $pdo = new PDO('mysql:host=localhost;dbname=auto', 'root', '');
  12. return $pdo;
  13. }
  14.  
  15. public function XMLtoDb()
  16. {
  17. $pdo = $this->Connect();
  18. $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><users></users>');
  19. $stmt = $pdo->query('SELECT login, password, fio, email, adres FROM users');
  20. while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  21. $user = $xml->addChild('user');
  22. foreach ($row as $key => $value) {
  23. $user->addChild($key, $value);
  24. }
  25. }
  26. echo $xml->asXML("result.xml");
  27. }
  28.  
  29. public function DbtoXML()
  30. {
  31. $pdo = $this->Connect();
  32. $string = file_get_contents('result.xml');
  33. $xml = new SimpleXMLElement($string);
  34. foreach($xml->user as $item){
  35. $login = $item->login;
  36. $password = $item->password;
  37. $fio = $item->fio;
  38. $email = $item->email;
  39. $adres = $item->adres;
  40. $stm = $pdo->prepare("INSERT INTO users (login, password, fio, email, adres) VALUES (? ,? ,?, ?, ?)");
  41. $stm->bindParam(1, $login);
  42. $stm->bindParam(2, $password);
  43. $stm->bindParam(3, $fio);
  44. $stm->bindParam(4, $email);
  45. $stm->bindParam(5, $adres);
  46. $users=$stm->execute();
  47. }
  48. }
  49. }
  50. $object = new User;
  51. $object->XMLtoDb();
  52. $object->DbtoXML();
  53. ?>
Add Comment
Please, Sign In to add comment