JSONkit, MySQL, iOS - how?

I searched and cannot find out how to grab data from a MySQL database using JSON in iOS. Of course, this will require a middle-aged PHP person, but how to do all this seems incomprehensible at the moment.

Can someone please help?

Thank!

+3
source share
1 answer

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

then in your ios use a parser (I use SBJSON) and it converts JSON to an array or a dictionary (depending on how you use it) ... I did not use JSONKit, so I can not comment on this aspect

: , "", ( JSON)? JSON iOS, ?

<?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 "]}";
?>
+1

All Articles