dongtanliefang8765 2016-08-24 22:36
浏览 162
已采纳

我应该如何将数组从iOS传递到PHP以便正确存储(并返回)?

I know this is a loaded question, but I think what I'm doing right now is probably the wrong way to go about things, so I'd like some input as to what is best practice.

Currently I have an array of actors. Let's say NSArray *actors in Objective-C. I want to pass this up to my PHP page for storage.

Right now I pass this up on HTTP POST by turning the array into a string such as this:

&actors=Joe Allen*Dave Smith*Mary Johnson*Alice Burg?movietitle=Runner

When it gets to PHP I'm then passing it to MySQL via a Stored Procedure call like this:

CALL Add_Movie(movietitle, actors);

And then in my sproc is where I 'explode' the actors by * to save them in the correct table. I do the reverse to get the actors back. It's gotten even trickier for me when I've wanted to separate first/last names.

Anyway, this seems really hacky to me. What's best practice for an array transfer over HTTP to SQL storage and back?

  • 写回答

3条回答 默认 最新

  • duancheng6221 2016-08-25 00:54
    关注
    1. Pick an established serialization format, don't try to roll your own.
      • Established formats have pre-written libraries to handle things like encoding complex types and encapsulating/escaping strings/data.
    2. Pick a serialization format that is well-defined and supported by both ends, that is IOS/ObjC and PHP.
      • You don't want to waste time writing new libraries to implement unsupported formats.
    3. Don't store serialized data in an RDBMS, particularly MySQL.
      • Storage, access, indexing, etc all become difficult or impossible to do or do well.
      • If you're simply looking for a key-value store there are much better options than MySQL.

    That said, there are two standout candidate formats:

    • JSON: The lingua franca of the modern internet. Simple, lightweight, flexible, and damn-near everything understands it.
    • XML: JSON's bloated uncle. Clunky, verbose, and generally a big pain in the ass; but if you want to impose typing, structure, and other such things on your data exchange format you could do worse than XML.

    In the big picture what you've described sounds like the exact case for defining an REST API.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗