2012-03-29 66 views
1

我目前正在嘗試構建一個需要能夠檢索和存儲遠程關係數據庫中的數據的ios應用程序。我調查了前端使用RestKit,這似乎是足夠的。我有使用mysql和php進行開發的經驗,但我不確定從哪裏開始讓Web服務器發送不是html的信息。 (PHP的增強的HTML,這是)後端iOS應用與關係數據庫

我將不勝感激我可能開始的任何輸入。提前致謝!

回答

0

我使用PHP查詢服務器上的mySQL數據庫,並將響應格式化爲XML,然後在應用程序中對其進行解析。 這裏是我的服務器的PHP示例....這也發送一個大小的響應,以允許應用程序跟蹤下載完成百分比和文件名稱響應。另外,如果數據庫爲空,它將發送虛擬記錄。

<?php 
    header("Content-type: text/text"); 

//get the values from the url query string 
$user = $_GET['user']; 
$pass = $_GET['password']; 
$db = $_GET['database']; 
$dbhost = 'localhost'; 

//create connection to mysql or tell user something has gone wrong 
$connection = mysql_connect($dbhost, $user, $pass) or die('Could not connect to MySQL'); 
//select database or tell user something has gone wrong 
mysql_select_db($db) or die('Could not select database'); 


// Construct and run query. 
    $sql = 'SELECT * FROM itemInfo WHERE itemStatus = "active" ORDER BY itemSortKey ASC'; 
    //echo $sql; 
    $result = mysql_query($sql) or die('<result>fail</result>'); 

    $xml_output = "<?xml version=\"1.0\"?>\n"; 
    $xml_output .= "<bodyimage>\n"; 


    while($record = mysql_fetch_assoc($result)) { 
     $xml_output .= "\t<storeitems>\n"; 
     $xml_output .= "\t\t<itemSortKey>" . $record['itemSortKey'] . "</itemSortKey>\n"; 
     $xml_output .= "\t\t<itemDesc>" . $record['itemDesc'] . "</itemDesc>\n"; 
     $xml_output .= "\t\t<itemPrice>" . $record['itemPrice'] . "</itemPrice>\n"; 
     $xml_output .= "\t\t<productID>" . $record['productID'] . "</productID>\n"; 
     $xml_output .= "\t\t<field1>" . $record['field1'] . "</field1>\n"; 
     $xml_output .= "\t\t<field2>" . $record['field2'] . "</field2>\n"; 
     $xml_output .= "\t\t<field3>" . $record['field3'] . "</field3>\n"; 
     $xml_output .= "\t\t<field4>" . $record['field4'] . "</field4>\n"; 
     $xml_output .= "\t\t<dateCreated>" . $record['dateCreated'] . "</dateCreated>\n"; 
     $xml_output .= "\t\t<dateModified>" . $record['dateModified'] . "</dateModified>\n"; 

    // Escaping illegal characters 
    //$row['text'] = str_replace("&", "&", $row['text']); 
    //$row['text'] = str_replace("<", "<", $row['text']); 
    //$row['text'] = str_replace(">", "&gt;", $row['text']); 
    //$row['text'] = str_replace("\"", "&quot;", $row['text']); 
    //$xml_output .= "\t\t<text>" . $row['text'] . "</text>\n"; 

     $xml_output .= "\t</storeitems>\n"; 

     //add this record to the output 
     //$output = '|facilityname|' .$record['facilityname'] . '|/facilityname||customername|' . $record['customername'] . '|/customername||imageurl|' . $record['imageurl'] . '|/imageurl|'; 
     //print $output; 
    } 
    $xml_output .= "</bodyimage>"; 

    header("HTTP/1.0 200 OK"); 
    header('Content-type: text/xml'); 
    header('Content-Disposition: attachment; filename="storeitems.xml"'); 
    header("Content-Length: ".strlen ($xml_output)); 

    // redundent.....$xml_output .= "</bmrcontrol>"; 

    //echo strlen($xml_output); 
    //the next statement relates if there are NO lines in the table - it sends a message 
    if (strlen($xml_output) == 61) { 
     $xml_output = "<?xml version=\"1.0\"?>\n"; 
     $xml_output .= "<bodyimage>\n"; 

     $xml_output .= "\t<storeitems>\n"; 
     $xml_output .= "\t\t<itemSortKey>aaaa</itemSortKey>\n"; 
     $xml_output .= "\t\t<itemDesc>Store Items Missing</itemDesc>\n"; 
     $xml_output .= "\t\t<itemPrice>0.0</itemPrice>\n"; 
     $xml_output .= "\t\t<productID>blank</productID>\n"; 
     $xml_output .= "\t\t<field1>.</field1>\n"; 
     $xml_output .= "\t\t<field2>.</field2>\n"; 
     $xml_output .= "\t\t<field3>.</field3>\n"; 
     $xml_output .= "\t\t<field4>.</field4>\n"; 
     $xml_output .= "\t\t<dateCreated>2012-02-13 09:03:49</dateCreated>\n"; 
     $xml_output .= "\t\t<dateModified>2012-02-13 09:03:49</dateModified>\n"; 

     // Escaping illegal characters 
     //$row['text'] = str_replace("&", "&", $row['text']); 
     //$row['text'] = str_replace("<", "<", $row['text']); 
     //$row['text'] = str_replace(">", "&gt;", $row['text']); 
     //$row['text'] = str_replace("\"", "&quot;", $row['text']); 
     //$xml_output .= "\t\t<text>" . $row['text'] . "</text>\n"; 

     $xml_output .= "\t</storeitems>\n"; 
     $xml_output .= "</bodyimage>"; 
    } 

    echo $xml_output; 

    if(mysql_num_rows($result)) { 
     //mysql_close($connection); 
    } 
    else 
     die('<result>fail</result>'); 
     //mysql_close($connection); 
?>