从sql获取记录计数并使用php将其传递给xml

时间:2017-12-21 07:58:50

标签: php mysql xml

我有以下查询从mysql表中搜索数据并使用php导出到XML,但我无法弄清楚如何将记录计数传递给xml中的MatchCount元素。

以下是我的剧本:

 <?php
header ("content-type: text/xml");
$connection = mysqli_connect('127.0.0.1', 'root', 'admin', 'Customer_1') or die("cannot connect");

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

$RegistrationMark = $_GET['RegistrationMark'];
$MachineName  = $_GET['MachineName'];



$qr= mysqli_query($connection, "SELECT * FROM `EarlsdonMSIN_anpr_vega` where `RegistrationMark` like '%" .$RegistrationMark. "%'");

$xml.='<CaptureResponse  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\CaptureResponse.xsd"> <MachineName>'.$_GET['MachineName'].'</MachineName><MatchCount>10</MatchCount><ResponseDateTime>2017-12-20T14:00:00</ResponseDateTime><MatchRecords ImageURI = "http://192.192.192.200/share/CACHEDEV1_DATA/Lanein1/EarlsdonMSIN/">';

while($res=mysqli_fetch_array($qr))
{
 $xml.='<MatchLine><VehicleRegistration>'.$res['RegistrationMark'].'</VehicleRegistration><LastStatus>IN</LastStatus><datetime>'.$res['datetime'].T.$res['time'].'</datetime><ImageFile>'.$res['image_name'].'</ImageFile></MatchLine>';
}

$xml.='</MatchRecords></CaptureResponse>';
echo $xml;
?>

2 个答案:

答案 0 :(得分:0)

你需要得到点数

$row_cnt = mysqli_num_rows($qr);

然后你需要连接结果

$xml.='<CaptureResponse  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\CaptureResponse.xsd"> <MachineName>'.$_GET['MachineName'].'</MachineName><MatchCount>'.$row_cnt.'</MatchCount><ResponseDateTime>2017-12-20T14:00:00</ResponseDateTime><MatchRecords ImageURI = "http://192.192.192.200/share/CACHEDEV1_DATA/Lanein1/EarlsdonMSIN/">';

答案 1 :(得分:0)

Php提供mysqli_num_rows()根据查询计算行数,因此您可以在mysqli_query()

之后使用它

试试这个,

 $MatchCount=mysqli_num_rows($qr);

然后将$MatchCount替换并连接到10。以下是一个例子

$xml.='<CaptureResponse  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\CaptureResponse.xsd"> <MachineName>'.$_GET['MachineName'].'</MachineName><MatchCount>'.$MatchCount.'</MatchCount><ResponseDateTime>2017-12-20T14:00:00</ResponseDateTime><MatchRecords ImageURI = "http://192.192.192.200/share/CACHEDEV1_DATA/Lanein1/EarlsdonMSIN/">';