ORDER BY ASC inside Array query

Hi, I’m new with Yii Framework, and a have one question!

I need insert one "ORDER BY column ASC" im my code

Here is it:


	public function subcategoryList()

	{

		$list='';

		$stmt="SELECT a.*

		       FROM {{subcategory}} a

		";		

		$connection=Yii::app()->db;

		$rows=$connection->createCommand($stmt)->queryAll(); 		

		if (is_array($rows) && count($rows)>=1){

			foreach ($rows as $val) {

				$stmt2="SELECT a.*,

				        (

				        select sub_item_name

				        from {{subcategory_item}}

				        where

				        sub_item_id=a.sub_item_id

				        ) as sub_item_name

				        FROM

				        {{subcat_relationship}} a

				        WHERE

				        subcat_id='".addslashes($val['subcat_id'])."'


				";

				$rows2=$connection->createCommand($stmt2)->queryAll(); 		

				$list[]=array(

				   'subcat_id'=>$val['subcat_id'],

				   'subcategory_name'=>$val['subcategory_name'],

				   'sub_item'=>$rows2

				);

			}

			return $list;

		}

		return FALSE;

	}

I wanted to insert the ORDER BY equal to that


	public function getSubcategoryItemFullDetails()

	{

		$data_feed='';

		$stmt="

		SELECT * FROM

		{{subcategory_item}}

		ORDER BY sequence ASC

		";

		$connection=Yii::app()->db;

		$rows=$connection->createCommand($stmt)->queryAll(); 				

		if (is_array($rows) && count($rows)>=1){			

			foreach ($rows as $val) {				   

			   $data_feed[$val['sub_item_id']]=$val;

			}

			return $data_feed;

		}

		return FALSE;

	}	

However, the code that i want to insert ORDER BY, uses Array, and i do not know how to do this,I’ve tried a number of ways, and I can’t, I’m doing something wrong! If anyone can help me, I’ll be very happy!

Hi Jean,

welcome to the Yii forum.

Apart from this ORDER BY problem, you have a bigger problem in your query, and that is you are not binding your query parameters, you are just concating the strings. This enables possible attackers to perform SQL Injection attacks.

Please read the following:

http://www.yiiframework.com/doc/guide/1.1/en/database.arr

Duri, Thank you so much!

Thanks for security alert, it is always good not to leave loopholes for invaders!

I think I know how I will solve this problem, thanks to its link Guide.

I’m glad I helped you ;)