PHP read and write to the database exemplary xml

<?php

$xml = simplexml_load_file('books.xml');
// print_r($xml);
// echo "<br/>";
$t = $xml->to;
$f = $xml->from;
$h = $xml->heading;
$b = $xml->body;

$dsn = "mysql:host=localhost;dbname=demo";
$username = "root";
$password = "00000000";
$conn = new PDO($dsn,$username,$password);
try{
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

$stmt = $conn->prepare("insert into xml(t,f,heading,body) values(:t,:f,:h,:b)");
$stmt->bindParam(':t',$t);
$stmt->bindParam(':f',$f);
$stmt->bindParam(':h',$h);
$stmt->bindParam(':b',$b);
$stmt->execute();
echo "success";
}catch(PDOException $e){
echo "Error" . $e->getMessage();
}
$conn = null;

 

xml file:

<?xml version="1.0" encoding="UTF-8"?>

<note>

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>

Guess you like

Origin www.cnblogs.com/connectionworld/p/11076617.html