2016-06-21 70 views
0

该脚本可以拉从MySQL表中的数据MySQL表和输出XML,但使用变量,然后将其导出为xml如何检索URL

我怎样才能检索到MSGID通过传递变量到URL并输出到XML

这应该很容易,但我似乎无法做到。

它应该是这样的:

http://localhost/test.php?msgid=d503dba6-44b8-4ba0-ae9a-8d6743a914ee

<data> 
<acceptreport> 
    <id>4692</id> 
    <username>test1</username> 
    <msgid>d503dba6-44b8-4ba0-ae9a-8d6743a914ee</msgid> 
    <sender>TOPS</sender> 
    <receiver>523452345</receiver> 
    <acceptedfordeliverytime>2016-06-21 09:04:16</acceptedfordeliverytime> 
    <deliveredtohandsettime>2016-06-21 09:04:00</deliveredtohandsettime> 
    <operator>AIRLINK</operator> 
    <status>deliveredtohandset</status> 
</acceptreport> 
</data> 

这是脚本

<?php 
//database configuration 
$config['mysql_host'] = "192.168.1.1"; 
$config['mysql_user'] = "test"; 
$config['mysql_pass'] = "pass#word"; 
$config['db_name'] = "testdb"; 
$config['table_name'] = "box"; 

//connect to host 
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']); 
//select database 
@mysql_select_db($config['db_name']) or die("Unable to select database"); 

/** 
* @param mysql_resource - $queryResult - mysql query result 
* @param string - $rootElementName - root element name 
* @param string - $childElementName - child element name 
*/ 
function sqlToXml($queryResult, $rootElementName, $childElementName) 
{ 
    $xmlData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"; 
    $xmlData .= "<" . $rootElementName . ">"; 

    while($record = mysql_fetch_object($queryResult)) 
    { 
     /* Create the first child element */ 
     $xmlData .= "<" . $childElementName . ">"; 

     for ($i = 0; $i < mysql_num_fields($queryResult); $i++) 
     { 
      $fieldName = mysql_field_name($queryResult, $i); 

      /* The child will take the name of the table column */ 
      $xmlData .= "<" . $fieldName . ">"; 

      /* We set empty columns with NULL, or you could set 
       it to '0' or a blank. */ 
      if(!empty($record->$fieldName)) 
       $xmlData .= $record->$fieldName; 
      else 
       $xmlData .= "null"; 

      $xmlData .= "</" . $fieldName . ">"; 
     } 
     $xmlData .= "</" . $childElementName . ">"; 
    } 
    $xmlData .= "</" . $rootElementName . ">"; 

    return $xmlData; 
} 

/* Sql query */ 
$result = mysql_query("SELECT id, username, msgid, sender, receiver, acceptedfordeliverytime, deliveredtohandsettime, operator, status FROM outbox WHERE username = 'top2' ORDER BY id DESC LIMIT 1"); 

/* If you want to process the returned xml rather than send it 
    to the browser, remove the below line. 
*/ 
header("Content-Type: application/xml"); 
echo sqlToXml($result, "data", "acceptreport"); 
?> 
+0

您确切需要什么? –

+0

我想在我的网址中使用一个变量来查询mysql表,然后将其输出到xml –

+0

在子标记中添加以下行:$ xmlData。=“”。$ _REQUEST ['msgid']。“”; –

回答

-1

你传递了​​错误的变量获得where子句中的数据。

$result = mysql_query("SELECT id, username, msgid, sender, receiver, acceptedfordeliverytime, deliveredtohandsettime, operator, status FROM outbox WHERE username = 'burs2' ORDER BY id DESC LIMIT 1"); 

您需要在username通过在URL中,让您的动态数据

$result = mysql_query("SELECT id, username, msgid, sender, receiver, acceptedfordeliverytime, deliveredtohandsettime, operator, status FROM outbox WHERE username = '".$_REQUEST['username']."' ORDER BY id DESC LIMIT 1"); 

你不是由msgid拉动数据。您应该呼叫的网址应该如下

http://localhost/test.php?username=test1 
+0

不要直接插入一个变量到mysql语句!请检查varibale首先有效的内容 – Andre

+0

哪个变量? –

+0

我需要通过msgid来取消用户名。我应该检查哪个变量? –