doushui3061 2014-10-31 07:41
浏览 61
已采纳

如何将PHP mysql_fetch_array()转换为NSMutableArray? PHP - > Xcode

I'm trying to read an entire row from mysql into a NSMutableArray using PHP.

Example PHP Code

<?php
$con = mysql_connect("localhost", "username", "password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("test_db",$con);
$sql = "SELECT * from Person WHERE Lastname='smith'";
$result = mysql_query($sql,$con);
echo(mysql_fetch_array($result));

mysql_close($con);
?>

The output of the array above could be:

Array
(
[0] => smith
[LastName] => smith
[1] => Adam
[FirstName] => Adam
[2] => California
[Address] => California
[3] => 22
[Age] => 22
)

How do I read that array from mysql, and store that as a NSMutableArray?

So far, I only know how to receive a string from PHP using the following:

//to execute php code
NSData *dataURL = [NSData dataWithContentsOfURL: [NSURL URLWithString:strURL]];

//to receive the returned value
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding: NSUTF8StringEncoding];

But I don't know how to read an array and convert that into a NSMutableArray. I spent hours searching and I cannot find an answer. I would prefer not to use JSON and stick with Mysql/PHP/Obj-c.

  • 写回答

1条回答 默认 最新

  • douqian6315 2014-10-31 08:47
    关注

    It is not so simply eheh. You have to transform the HTML data in something that iOS can understand. Of course, if you do the var_dump() of the $result array is not something that iOS can understand! You have to convert the string in a universal format like JSON or XML, and then download the data. So, when iOS receive the JSON/XML, you can handle it using reflection or something else to reproduce the JSON objects.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料