从表中选择1,奇怪的print_r结果

时间:2022-08-28 23:32:07

From what I have read, when executing the following sql command and fetchAll() on my table with 6 rows and 11 columns:

根据我所读到的,在我的表上执行下面的sql命令和fetchAll()时,有6行11列:

$sql = "SELECT 1 FROM table";
$sqlPrepared = $conn->prepare($sql)
$sqlPrepared->execute()
$result = $sqlPrepared->fetchAll();
$print_r($result);

I should be getting 6 rows of just one value in each row, with the value 1 inside each of those values. However, I am getting 6 rows of two values in each row, with the value 1 inside each of those values:

我应该在每一行中得到6行值,每一行的值都是1。但是,每一行有6行两个值,每个值中都有1个值:

Array ( 
  [0] => Array ( 
    [1] => 1 
    [2] => 1 ) 
  [1] => Array ( 
    [1] => 1 
    [2] => 1 ) 
  [2] => Array ( 
    [1] => 1 
    [2] => 1 ) 
  [3] => Array ( 
    [1] => 1 
    [2] => 1 ) 
  [4] => Array ( 
    [1] => 1 
    [2] => 1 ) 
  [5] => Array ( 
    [1] => 1 
    [2] => 1 ) 
) 

QUESTION 1: Why am I getting 2 values for each array instead of just 1?

问题1:为什么每个数组都有2个值而不是1?

QUESTION 2: Instead of the inner arrays being

问题2:而不是内部数组。

 Array ( 
    [1] => 1 ...

why doesn't it start from [0]?:

为什么不从[0]开始呢?

 Array ( 
    [0] => 1 ...

1 个解决方案

#1


4  

If you don't provide any flag on ->fetchAll() method, this includes the associative and numeric indices on the multidimensional array row.

如果您没有在>fetchAll()方法中提供任何标志,这包括多维数组行中的关联和数值索引。

So when you used SELECT 1 FROM, the associative index is 1 (meaning, column name is 1), and since array keys are unique, the numeric index adjusted, the numeric index with the value pair is assigned to 2.

因此,当您使用SELECT 1 FROM时,关联索引为1(意思是,列名为1),并且由于数组键是惟一的,因此调整了数值索引,具有值对的数值索引被分配为2。

#1


4  

If you don't provide any flag on ->fetchAll() method, this includes the associative and numeric indices on the multidimensional array row.

如果您没有在>fetchAll()方法中提供任何标志,这包括多维数组行中的关联和数值索引。

So when you used SELECT 1 FROM, the associative index is 1 (meaning, column name is 1), and since array keys are unique, the numeric index adjusted, the numeric index with the value pair is assigned to 2.

因此,当您使用SELECT 1 FROM时,关联索引为1(意思是,列名为1),并且由于数组键是惟一的,因此调整了数值索引,具有值对的数值索引被分配为2。