Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中将XML子节点值保存到数据库_Php_Mysql_Xml - Fatal编程技术网

如何在PHP中将XML子节点值保存到数据库

如何在PHP中将XML子节点值保存到数据库,php,mysql,xml,Php,Mysql,Xml,我需要关于将XML数据保存到MySQL数据库的帮助。这是我的密码: <?xml version="1.0" encoding="UTF-8"?><Response Code="200"><Description>http://sample.net</Description><URL>/Patient/PatientView.aspx?pid=642</URL></Response> http://sam

我需要关于将XML数据保存到MySQL数据库的帮助。这是我的密码:

    <?xml version="1.0" encoding="UTF-8"?><Response Code="200"><Description>http://sample.net</Description><URL>/Patient/PatientView.aspx?pid=642</URL></Response>
http://sample.net/Patient/PatientView.aspx?pid=642
现在我想做的是获取
标记和
标记的值,并将它们组合成一个完整的url,然后将其保存到mysql数据库中。

请参见此处的问题

您可以使用SimpleXML(请参阅)进行解析

$xmlStr = '<?xml version="1.0" encoding="UTF-8"?><Response Code="200"><Description>http://sample.net</Description><URL>/Patient/PatientView.aspx?pid=642</URL></Response>';

$response = new SimpleXMLElement($xmlStr);

$url = (string) $response->Description . (string) $response->URL;
然后使用
PDO
()将数据存储到数据库中:

try {
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    $stmt = $dbh->prepare("INSERT INTO sample (url) VALUES (:url)");
    $stmt->bindParam(':url', $url);
    $stmt->execute();
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
试试看{
$dbh=newpdo('mysql:host=localhost;dbname=test',$user,$pass);
$stmt=$dbh->prepare(“插入样本(url)值(:url)”);
$stmt->bindParam(':url',$url);
$stmt->execute();
}捕获(PDO$e){
打印“错误!:”$e->getMessage()。“
”; 模具(); }
您需要先解析xml,使用
simplexml
完成解析后,再使用pdo连接并进行插入谢谢我试试您的建议,先生:-)谢谢@Sergey Kasatkin,我试试这个。:-)
try {
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    $stmt = $dbh->prepare("INSERT INTO sample (url) VALUES (:url)");
    $stmt->bindParam(':url', $url);
    $stmt->execute();
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}