MySQL PDO fetchAll as array with integer index

The method you have is fine. Though if you don't need the ID, why would you need to query it?

<?php
    $sth = $dbh->prepare("SELECT ctg FROM ctgtable");
    $sth->execute();
    /* Fetch all of the values in form of a numeric array */
    $result = $sth->fetchAll(PDO::FETCH_ARRAY);
    var_dump($result);
?>

Less constraints on the MySQL leads to less processing time, which eventually leads to better results.


You can simply do the following

   <?php
    //connect to db    

    $array = array();//define array

    $query = "SELECT * FROM ctgtable";    
    $result = $pdo->prepare($query);
    $result->execute();

   while ($row = $result->fetch()) {
      $id =  $row['id'];
      $ctg = $row['ctg'];        
      $array[$id] = $ctg;        
  }

 print_r($array);
 //close connection
?>