php查询数据返回xml php返回值( 二 )


var price=elements[i].getElementsByTagName('price')[0].firstChild.nodeValue;
str+= 'trtd'+id+'/tdtd'+name+'/tdtd'+date+'/tdtd'+price+'/td/tr';
}
document.getElementById('table2').innerHTML=str;
}
};
xhr.send(null);
}
/script
button onclick="check();"点我/button
table id="table2" border="2" cellspacing="0"
/table
/body
/html
效果
1
2
补充:
使用JSON
1数据库查询,自己拼 JSON 编码
?php
$dsn="mysql:host=localhost;dbname=test";
try {
$pdo = new PDO($dsn,'root','password'); //初始化一个PDO对象 , 就是创建了数据库连接对象$pdo
$query="select * from book";//定义SQL语句
$pdo-query('set names utf8');
$result=$pdo-prepare($query); //准备查询语句
$result-execute();//执行查询语句,并返回结果集
$a=$arr='';
while($res=$result-fetch()){
$arr.='{"id":'.'"'.$res[0].'",'.'"name":'.'"'.$res[1].'",'.'"time":'.'"'.$res[2].'",'.'"jia":'.'"'.$res[3].'",'.'"zhe":'.'"'.$res[4].'",'.'"chu":'.'"'.$res[5].'"},';
}
echo $a="[".substr($arr,0,strlen($arr)-1)."]";
} catch (PDOException $e) {
die ("Error!: ".$e-getMessage()."br");
}
?
2数据库查询 , 函数 JSON 编码
?php
$dsn="mysql:host=localhost;dbname=test";
try {
$pdo = new PDO($dsn,'root','password'); //初始化一个PDO对象 , 就是创建了数据库连接对象$pdo
$query="select * from book";//定义SQL语句
$pdo-query('set names utf8');
$result=$pdo-prepare($query); //准备查询语句
$result-execute();//执行查询语句,并返回结果集
$res=$result-fetchAll();
//JSON 编码
echo json_encode($res);
} catch (PDOException $e) {
die ("Error!: ".$e-getMessage()."br/");
}
?
效果
3ajax获?。?JSON解析
!DOCTYPE html
html lang="en"
head
meta charset="UTF-8"
titleJSON/title
/head
body
script
function check() {
var XHR = new XMLHttpRequest();
XHR.open('GET','JSON.php');
XHR.onreadystatechange = function (){
if(XHR.readyState == 4XHR.status ==200){
var books =JSON .parse(XHR.responseText);
var books2='trthid/ththbookname/ththtime/ththprice/ththmarker/ththpublisher/th/tr'
for (var i=0;ibooks.length;i++){
books2+= 'trtd'+(books[i ].id)+'/tdtd'+(books[i ].name)+'/tdtd'+(books[i ].time)+'/tdtd'+(books[i ].jia)+'/tdtd'+(books[i ].zhe)+'/tdtd'+(books[i ].chu)+'/td/tr';
}
document.getElementById('table2').innerHTML=books2;
}
};
XHR.send(null);
}
/script
input type="button" value="https://www.04ip.com/post/点我" onclick="check();"
table id="table2" border="2" cellspacing="0"/table
/body
/html
PHP怎么解析微信支付结果返回的xmlphp解析xml报文的方法是DOMDocument:
解析方法如下:
$xmlstring = XML
?xml version='1.0'?
document
cmd attr='default'login/cmd
loginimdonkey/login
/document
XML;
$dom = new DOMDocument();
$dom-loadXML($xmlstring);
print_r(getArray($dom-documentElement));
function getArray($node) {
$array = false;
if ($node-hasAttributes()) {
foreach ($node-attributes as $attr) {
$array[$attr-nodeName] = $attr-nodeValue;
}
}
if ($node-hasChildNodes()) {
if ($node-childNodes-length == 1) {
$array[$node-firstChild-nodeName] = getArray($node-firstChild);
} else {
foreach ($node-childNodes as $childNode) {
if ($childNode-nodeType != XML_TEXT_NODE) {
$array[$childNode-nodeName][] = getArray($childNode);
}
}
}
} else {
return $node-nodeValue;
}
return $array;
}
php查询数据返回xml的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于php返回值、php查询数据返回xml的信息别忘了在本站进行查找喔 。

推荐阅读