2016-06-21 35 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來取消用戶名。我應該檢查哪個變量? –