Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8556935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:27:43+00:00 2026-06-11T15:27:43+00:00

I have a table with two columns. Table: ctgtable and columns: id and ctg

  • 0

I have a table with two columns. Table: ctgtable and columns: id and ctg. Since I am entirely moving from previous mysql_* functions to PDO, I am facing some foolish mistakes(or maybe lack of knowledge).

Question

I want to select the entire table’s ctg column(a total of at most 20 rows) into an array with integer indexes.

My Method

The nearest possible solution in my opinion was this:

<?php
    $sth = $dbh->prepare("SELECT id, ctg FROM ctgtable");
    $sth->execute();
    /* Fetch all of the values of the second column */
    $result = $sth->fetchAll(PDO::FETCH_COLUMN, 1);
    var_dump($result);
?>

Is there any other shorter/better alternative for the same result? Or this is the best/only possible method to fetch results.

Sample Table

id      ctg
01     movie
27       tv
64     sports

etc.

Sample Result

Array( 1 => "tv",
    2 => "movie",
    3 => "anime",
    4 => "game",
    5 => "telugu"
);

The indexing may or may not start from 0. It doesn’t matter to me. I tried searching for such a possible question, but none of them seemed relevant to my question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:27:44+00:00Added an answer on June 11, 2026 at 3:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table with two columns (varchar from, varchar to). This table represents connections
I have table with two columns (startdate & enddate) and I have two submission
I have a table with two columns: person_id person_id with whom 1st field id
I have a table with two columns and one row, and 100% width across
I have a table with two columns: column A column B 1 2 1
I have a table with two columns, GroupId and ParentId (both are GUIDS). The
I have a table with a few thousand rows. The table contains two columns,
I have a table TableA with two columns: MyName nvarchar(100) and MyDateTime DateTime .
I have a tree table with two columns on a page. The first one
I have a table that has two columns: CommunityID PersonID And A People table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.