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 8013345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:36:38+00:00 2026-06-04T19:36:38+00:00

I have a table called Category with a few columns and I’m trying to

  • 0

I have a table called Category with a few columns and I’m trying to get only a few out of my database.

So I’ve tried this:

$sql = 'SELECT uppercat AS id, COUNT(uppercat) AS uppercat FROM category GROUP BY uppercat;';
$d = Yii::app()->db->createCommand($sql)->query();

But I find the output strange. I was trying to do an array_shift but I get an error that this isn’t an array. When I do a var_dump on $d:

object(CDbDataReader)[38]
  private '_statement' => 
    object(PDOStatement)[37]
      public 'queryString' => string 'SELECT uppercat AS id, COUNT(uppercat) AS uppercat FROM category GROUP BY uppercat;' (length=100)
  private '_closed' => boolean false
  private '_row' => null
  private '_index' => int -1
  private '_e' (CComponent) => null
  private '_m' (CComponent) => null

Ok.. then I did a foreach on $id:

array
  'id' => string '0' (length=1)
  'uppercat' => string '6' (length=1)
array
  'id' => string '3' (length=1)
  'uppercat' => string '2' (length=1)
array
  'id' => string '6' (length=1)
  'uppercat' => string '1' (length=1)
array
  'id' => string '7' (length=1)
  'uppercat' => string '2' (length=1)
array
  'id' => string '9' (length=1)
  'uppercat' => string '2' (length=1)

Then why do I get the message that $id isn’t an array while it contains arrays?

Is there any other way on how to get some specific data out of my database and that I can then do an array_shift on them? I’ve also tried doing this with findAllBySql but then I can’t reach my attribute for COUNT(uppercat) which is not in my model. I guess I’d have to add it to my model but I wouldn’t like that because I need it just once.

  • 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-04T19:36:39+00:00Added an answer on June 4, 2026 at 7:36 pm

    CDbCommand’s query returns a CDbDataReader object for fetching the query result. Use queryAll instead, that returns an array of rows.

    However, it is nicer if you do it with CDbCriteria (you need a model property for that, you’re right).

    It would look something like this, assuming the property is called countUppercat.

    $criteria = new CDbCriteria;
    $criteria->select = 'uppercat, COUNT(uppercat) AS countUppercat';
    $criteria->group = 'countUppercat';
    $models = CategorieModel::model()->findAll($criteria);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL table called "posts" that looks like this: id | category
I've got a (SQL Server) database table called Category. And another database table called
I have a database with a table called shuffledQuestions with 2 columns category_id and
I have this data on a table called messages (id, category, user) : 1
I have a table called courses. The fields are: id, category, sortorder, fullname, etc.....
I have a table called ProjectRegion. It has two columns, an id and a
I have a table called scheduler_sched which has several columns, including a column called
I have a model called Category which looks like this: class Category < ActiveRecord::Base
I have a table called Token in my database that represents texts tokenized. Each
So, I am modeling a database using MySQL Workbench. I have a table called

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.