0

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

$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:

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?

QUESTION 2: Instead of the inner arrays being

 Array ( 
    [1] => 1 ...

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

 Array ( 
    [0] => 1 ...
Kevin
  • 41,694
  • 12
  • 53
  • 70
Webeng
  • 7,050
  • 4
  • 31
  • 59
  • 1 should be the column name or do you mean `*`? – aldrin27 Apr 20 '16 at 03:04
  • @aldrin27 1 is not the column name. "SELECT 1 FROM", it is a commonly used sql technique, which output is giving me almost what I expected it to, the value of "1" for each row that is found, however I am getting 2 columns of "1" for an odd reason. – Webeng Apr 20 '16 at 03:07
  • 1
    I think 1 checks whether it exists or not. http://stackoverflow.com/questions/7171041/what-does-it-mean-by-select-1-from-table – aldrin27 Apr 20 '16 at 03:12

1 Answers1

4

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

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.

Kevin
  • 41,694
  • 12
  • 53
  • 70