简体   繁体   English

在 PDO fetchAll() 中组合 FETCH_OBJ 和 FETCH_ASSOC 可能吗?

[英]Is combining FETCH_OBJ And FETCH_ASSOC in PDO fetchAll() possible?

I am trying to migrate from mysqli procedural to PDO because my website was halfway, half in pdo, and the rest in mysqli procedural, now I want to shift to PDO completely.我正在尝试从 mysqli 程序迁移到 PDO,因为我的网站是中途,一半在 pdo,rest 在 mysqli 程序,现在我想完全转移到 PDO。 Here is an example of the code I run这是我运行的代码示例

$rowNum = 0;
foreach ($result->fetchAll(PDO::FETCH_ASSOC|PDO::FETCH_OBJ) as $row) {
  $rowNum = $rowNum + 1;
   $dbUsername = $row['Username'];
}
if ($row>0) {
    echo $dbUsername;
}

But in some scenarios, the code gives me an error that trying to get property 'Username' of non-object但在某些情况下,代码会给我一个错误, trying to get property 'Username' of non-object

I know it was possible to use only ($result->fetchAll(PDO::FETCH_ASSOC)我知道可以只使用($result->fetchAll(PDO::FETCH_ASSOC)

But doing this ($result->fetchAll(PDO::FETCH_ASSOC|PDO::FETCH_OBJ) becomes a need as some context of the code I'm modifying use the symbol like $row->Usename and the other use $row['Username '], How can I make it accept both modes as shown above?但是这样做($result->fetchAll(PDO::FETCH_ASSOC|PDO::FETCH_OBJ)成为一种需要,因为我正在修改的代码的某些上下文使用$row->Usename之类的符号,而另一个使用$row['Username '],如何让它接受如上所示的两种模式?

I tried to use PDO:: FETCH_BOTH but the problem persists.我尝试使用 PDO:: FETCH_BOTH 但问题仍然存在。

As @YourCommonSense pointed out in his comment, you can use PDO::FETCH_LAZY to accomplish this.正如@YourCommonSense 在他的评论中指出的那样,您可以使用PDO::FETCH_LAZY来完成此操作。 However, you cannot use that with fetchAll , only with fetch :但是,您不能将它与fetchAll一起使用,只能与fetch一起使用:

while ($row = $result->fetch(PDO::FETCH_LAZY)) {
    // Both of these will work
    $username = $row['Username'];
    $username = $row->Username;
}

If you really want to use fetchAll , you'll have to fetch the rows as one type and cast them back and forth between arrays and objects:如果您真的想使用fetchAll ,则必须将行作为一种类型获取并在 arrays 和对象之间来回转换:

foreach ($result->fetchAll(PDO::FETCH_ASSOC) as $row) {
    // with PDO::FETCH_ASSOC, $row is an array
    $username = $row['Username'];

    $row = (object)$row;
    // $row is an object now
    $username = $row->Username;

    $row = (array)$row;
    // $row is now an array again
    $username = $row['Username'];
}

It's not possible.这是不可能的。 You cannot get both at the same time.您不能同时获得两者。 There would be no way to represent such a result in PHP.没有办法在 PHP 中表示这样的结果。

When you try to use both with PDO::FETCH_ASSOC|PDO::FETCH_OBJ you are actually fetching the result as PDO::FETCH_COLUMN .当您尝试将两者与PDO::FETCH_ASSOC|PDO::FETCH_OBJ一起使用时,您实际上是在获取结果PDO::FETCH_COLUMN That's because fetch parameter is a bitwise flags parameter.那是因为 fetch 参数是一个按位标志参数。 When you do an OR operation on these flags it is the same as 2|5 = 7 .当您对这些标志执行 OR 运算时,它与2|5 = 7相同。 7 is the value for PDO::FETCH_COLUMN . 7 是PDO::FETCH_COLUMN的值。

I don't know what use case you have for this, but it sounds like an XY problem.我不知道你有什么用例,但这听起来像是一个 XY 问题。 If you use fetchAll() you cannot have it both as an array and an object. But if you are fetching it row by row with fetch() , you could fetch each row in a different way.如果您使用fetchAll() ,则不能同时将其作为数组和 object。但是如果您使用fetch()逐行获取它,则可以用不同的方式获取每一行。

$res = $pdo->query("SELECT 1 as foo UNION ALL SELECT 2 as foo");
var_dump($res->fetch(PDO::FETCH_ASSOC));
var_dump($res->fetch(PDO::FETCH_OBJ));
/*
Outputs:
array(1) {
  ["foo"]=>
  int(1)
}
object(stdClass)#3 (1) {
  ["foo"]=>
  int(2)
}
*/

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

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