2012-03-29 5 views
1

В настоящее время я пытаюсь создать приложение ios, которое должно будет иметь возможность извлекать и хранить данные в удаленной реляционной базе данных. Я исследовал использование RestKit для переднего конца, что кажется адекватным. У меня есть опыт разработки как с mysql, так и с php, хотя я не уверен, с чего начать веб-сервер для отправки информации, которая не является 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); 
?> 
Смежные вопросы