数据库以对象或数组形式返回记录?
哪个是更好的选择? 我注意到 PDO 让您以数组或对象的形式返回。 在使用旧函数 (mysql_fetch_assoc()) 时,我总是使用数组,但我刚刚为 PDO 对象编写了一个包装类,我很好奇哪个更好。
我想一个对象会更严格......您可以更改/添加到返回的数组,这可能会意外地混淆您的结果(如果您忘记添加/更改了键/值)。
数组中返回的记录是否更老式的方式,我应该采用这些对象,出于什么原因?
Which is the better alternative? I've noticed PDO let's you return as an array or an object. I've always used arrays when using my old functions (mysql_fetch_assoc()) but I've just written a wrapper class for the PDO object and I was curious as to which is better.
I suppose an object would be stricter... you can change/add to a returned array which may muddle up your results unexpectedly (if you forgot you added/changed a key/value).
Are returned records in arrays more an old fashioned way, and should I adopt the objects, for which reasons?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 php.net:mysql_fetch_object
() 与 mysql_fetch_array() 类似,但有一个区别 — 返回一个对象,而不是一个数组。 间接地,这意味着您只能通过字段名称访问数据,而不能通过它们的偏移量(数字是非法属性名称)。
性能注意事项:
就速度而言,该函数与 mysql_fetch_array() 相同,并且几乎与 mysql_fetch_row() 一样快(差异微不足道)。
From php.net:
mysql_fetch_object() is similar to mysql_fetch_array(), with one difference — an object is returned, instead of an array. Indirectly, that means that you can only access the data by the field names, and not by their offsets (numbers are illegal property names).
Note on Performance:
Speed-wise, the function is identical to mysql_fetch_array(), and almost as quick as mysql_fetch_row() (the difference is insignificant).