简体   繁体   English

PDO :: FETCH_CLASS模式可以用于一行(不是fetchAll)吗?

[英]Can PDO::FETCH_CLASS mode be used for one row (not fetchAll)?

I feel a little dirty asking this here, since I feel I should've just been able to Google it, but well... I didn't find anything. 我在这里问这个问题有点不高兴,因为我觉得我应该已经可以用Google了,但是...我什么都没找到。

Is there a way to use the fetchmode PDO::FETCH_CLASS in the normal fetch function? 有没有办法在正常的fetch功能中使用fetch模式PDO::FETCH_CLASS I want to retrieve a single row, so using fetchAll is out of the question (performance issues). 我想检索一行,所以使用fetchAllfetchAll的(性能问题)。

I know I could use setFetchMode(PDO::FETCH_CLASS, "MyClass") , but ideally I would prefer not to change the default fetch mode. 我知道我可以使用setFetchMode(PDO::FETCH_CLASS, "MyClass") ,但理想情况下,我不希望不更改默认的获取模式。 I realise that I could always manually set it back to PDO::FETCH_BOTH afterwards, but it's not quite as neat as I'd like (extra function calls, code clutter, etc.). 我意识到,以后我总是可以手动将其设置回PDO::FETCH_BOTH ,但是它不像我想要的那样整洁(额外的函数调用,代码混乱等)。

If it isn't possible or the extra function calls don't matter at all that is fine too. 如果不可能,或者额外的函数调用也没有关系,那也没关系。 I was just wondering since it seems such an obvious thing to want to do. 我只是想知道,因为似乎很想做。

PS PDO::fetch() does seem to accept PDO::FETCH_CLASS as it's first argument, but I can't find a way to pass the class-identifier string in... PS PDO::fetch()似乎确实接受PDO::FETCH_CLASS作为它的第一个参数,但是我找不到在...中传递类标识符字符串的方法。

The answer seems to be no. 答案似乎是否定的。 No there is no such function. 不,没有这样的功能。

Use either setFetchMode(PDO::FETCH_CLASS, "MyClass") with your statement, or use fetchAll((PDO::FETCH_CLASS, "MyClass") . 在您的语句中使用setFetchMode(PDO::FETCH_CLASS, "MyClass") ,或使用fetchAll((PDO::FETCH_CLASS, "MyClass")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM