2012-03-29 10 views
1

現在、リモートのリレーショナルデータベースにデータを取得して保存する必要があるiosアプリケーションを構築しようとしています。私はフロントエンドにRestKitを使って調査しましたが、これは適切だと思われます。私は、HTMLではない情報を送信するためにWebサーバを取得する際にどこから始めるべきかわかりませんが、mysqlとphpの両方で開発経験があります。 (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); 
?> 
関連する問題