【问题标题】:Get record count from sql and pass it to xml using php从 sql 获取记录数并使用 php 将其传递给 xml
【发布时间】:2017-12-21 07:58:49
【问题描述】:

我有以下查询,它从 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;
?>

【问题讨论】:

  • 在哪里执行查询,知道什么是字符串拼接,知道sql可以做什么吗?
  • 请查看我编辑的查询
  • 你想显示行数吗?
  • 是的,请开发者先生

标签: php mysql xml


【解决方案1】:

你需要得到计数

$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/">';

【讨论】:

  • 你能告诉我如何在 xml.. 的 resposedatetime 上获取当前时间
  • 是的,再次感谢。
【解决方案2】:

php providemysqli_num_rows() 用于根据查询计算行数,所以你可以在你的mysqli_query()之后使用它

试试这个,

 $MatchCount=mysqli_num_rows($qr);

然后用10 替换并连接$MatchCount。下面是一个例子

$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/">'; 

【讨论】:

    猜你喜欢
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 2018-02-05
    • 2014-05-08
    • 2021-12-05
    • 1970-01-01
    相关资源
    最近更新 更多