php :服务器的编写,链接MYSQL 数据库,使用DOM ,输出XML 文件,并解析XML 文件
主要的代码部分如下:
$ num1 = $ _ GET [“number”]; // 获取前段学号数据
$ conn = new mysqli($ servername,$ username,$ password,$ dbname); // 连接数据库
$ xmlTag = array(
' 学号',
' 姓名',
' 数据结构',
'C 语言',
' 数据库系统',
); // 建立XML 标签
XML 的输出
$ dom = new DOMDocument('1.0','utf8'); // 新建dom 对象
$ dom-> formatOutput = true; // 格式化输出
$ student = $ dom-> createElement('student'); // 创建子节点学生
$ dom->的appendChild($学生); // 将学生的节点添加至根节点DOM 之下剩余节点依次类推
$ dom->保存( './ chaxun123.xml'); // 输出至路径下的chaxun123.xml 文件下
下面是代码的全部:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>服务器</title>
</head>
<body>
<?php
header('Content-Type:text/html;charset=UTF-8');
sleep(3);//睡眠3秒
$num1 =$_GET["number"];//获取前段学号数据
$servername = "127.0.0.1";//数据库地址
$username = "root";//数据库用户名
$password = "";//数据库密码
$dbname = "test1";//数据库名称
// 创建连接
$conn = new mysqli($servername, $username, $password,$dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
//echo "连接成功";
$sql = "SELECT * FROM student WHERE number = " . $num1 .";";//查询成绩的SQL语句
$res = mysqli_query($conn, $sql);//查询出来的结果
$row = mysqli_fetch_array($res);//对结果进行数组化抓取
if (!$row) {
printf("Error: %s\n", mysqli_error($conn));
exit();
}
$xmlTag = array(
'学号',
'姓名',
'数据结构',
'c语言',
'数据库系统',
);//建立XML文件
$dom = new DOMDocument('1.0', 'utf8');
$dom->formatOutput = true; //格式化输出
$student = $dom->createElement('student');//创建子节点
$dom->appendChild($student);//将student节点添加至根节点之下
$基本信息=$dom->createElement('基本信息');//创建子节点
$student->appendChild($基本信息);
$学号=$dom->createElement('学号');//创建子节点学号
$基本信息->appendChild($学号);//将学号节点放置到基本信息节点之下
$no1=$dom->createTextNode($row[0]);//创建文本节点并输入文本数据
$学号->appendChild($no1);//将数据放置到学号节点之下
$姓名=$dom->createElement('姓名');//创建子节点姓名
$基本信息->appendChild($姓名);
$no2=$dom->createTextNode($row[1]);
$姓名->appendChild($no2);
$数据结构=$dom->createElement('数据结构');//创建子节点数据结构成绩
$基本信息->appendChild($数据结构);
$no3=$dom->createTextNode($row[2]);
$数据结构->appendChild($no3);
$c语言=$dom->createElement('c语言');//创建子节点c语言成绩
$基本信息->appendChild($c语言);
$no4=$dom->createTextNode($row[3]);
$c语言->appendChild($no4);
$数据库系统=$dom->createElement('数据库系统');//创建子节点数据库系统成绩
$基本信息->appendChild($数据库系统);
$no5=$dom->createTextNode($row[4]);
$数据库系统->appendChild($no5);
$dom->save('./chaxun123.xml'); //输出至路径下的chaxun123.xml文件下
/*$doc = new DOMDocument();//创建新的DOM节点
$doc->load('./chaxun123.xml');//读取本地成绩XML
$student1=$doc->getElementById("基本信息");//解析XML文件,提取关键信息
$num11=$基本信息->getElementsByTagName("学号");
$学号=$num11->item(0)->nodeValue;
$num12=$基本信息->getElementsByTagName("姓名");
$姓名=$num12->item(0)->nodeValue;
$num13=$基本信息->getElementsByTagName("数据结构");
$数据结构=$num13->item(0)->nodeValue;
$num14=$基本信息->getElementsByTagName("c语言");
$c语言=$num14->item(0)->nodeValue;
$num15=$基本信息->getElementsByTagName("数据库系统");
$数据库系统=$num15->item(0)->nodeValue;
//输出学生信息表格至前台
//echo "<table border = '1'>
//<tr>
//<th>学号</th>
//<th>姓名</th>
//<th>数据结构</th>
//<th>c语言</th>
//<th>数据库系统</th>
//</tr>";
//echo '<tr>';
//echo '<td>' .$学号. '</td>';
//echo '<td>' . $姓名 .'</td>';
//echo '<td>' . $数据结构 .'</td>';
//echo '<td>' . $c语言 .'</td>';
//echo '<td>' . $数据库系统 .'</td>';
//echo '</td>';
//echo '</table>';*/
echo '查询成功';
$conn -> close();//关闭数据库连接
?><br>
</body>
</html>