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 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试