开发者

JSONkit, MySQL, iOS -- how?

I've been searching and can't find out how to grab data from a MySQL database using JSON in iOS. Of course this will require a开发者_运维百科 PHP middle-man, but how to do all of this seems unsearchable at this point.

Please could someone help?

Thank you!


depending on your php version you can json_encode() the results of your MySQL query, or (as in the below) you can hand craft the JSON.

then in your ios, use a parser (I use SBJSON) and it will convert the JSON into an array or dictionary (depending on how you use it)...I haven't used JSONKit so can't comment on that aspect

EDIT: Oh, actually, is that the 'direction' you meant (receive JSON)? or did you want to submit JSON from iOS to initiate the query?

<?php
//connect to DB       
$query = "SELECT * FROM table_name"; 
$result = mysql_query("$query");
echo "{\"results\":";
if($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
echo "[";
    echo "{\"field_one\":\"".$row["field_one"]."\",";
    echo "\"field_two\":\"".$row["field_two"]."\",";
    echo "\"field_three\":\"".$row["field_three"]."\",";
    echo "\"field_four\":\"".$row["field_four"]."\"";
    echo "}";
}
else{
echo "\"no\"}";
exit;
}
while($row = mysql_fetch_array($data,MYSQL_ASSOC)){
    echo ",{\"field_one\":\"".$row["field_one"]."\",";
    echo "\"field_two\":\"".$row["field_two"]."\",";
    echo "\"field_three\":\"".$row["field_three"]."\",";
    echo "\"field_four\":\"".$row["field_four"]."\"";
    echo "}";
}
echo "]}";
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜