Yii Framework Forum: Array problem - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Array problem Rate Topic: -----

#1 User is offline   neel 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 133
  • Joined: 08-October 09

Posted 12 April 2010 - 12:58 PM

Hey Guys
I have problem to print data array, here is my sql command
$P = Yii::app()->db->createCommand("SELECT test FROM Table WHERE table1_id = 1 ")->queryScalar();

I want to print
<?php echo $P[0];
echo $P[1];
........
?>

Can you tell me how to define sql command array and print like that?
0

#2 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 12 April 2010 - 01:32 PM

queryScalar() returns a single value. queryAll() is what you want I guess, it returns array.

In order to get a number-indexed array, you must use it this way:

$P = Yii::app()->db->createCommand("SELECT test FROM Table WHERE table1_id = 1 ")->queryAll(false);

0

#3 User is offline   neel 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 133
  • Joined: 08-October 09

Posted 12 April 2010 - 01:53 PM

View PostY!!, on 12 April 2010 - 01:32 PM, said:

queryScalar() returns a single value. queryAll() is what you want I guess, it returns array.

In order to get a number-indexed array, you must use it this way:

$P = Yii::app()->db->createCommand("SELECT test FROM Table WHERE table1_id = 1 ")->queryAll(false);




when I print
<item>' .$P[0]. '</item>
<item>' .$P[1]. '</item>
<item>' .$P[2]. '</item>

...
I got this error

The value [Array] is not valid for this field. It must contain an integer.

here is my var_dump result

var_dump ($P);

array(7) { [0]=> array(1) { ["i"]=> string(3) "700" } [1]=> array(1) { ["i"]=> string(3) "701" } [2]=> array(1) { ["i"]=> string(3) "702" } [3]=> array(1) { ["i"]=> string(3) "703" } [4]=> array(1) { ["i"]=> string(3) "704" } [5]=> array(1) { ["i"]=> string(3) "750" } [6]=> array(1) { ["i"]=> string(2) "91" } }


I want to print this way

when I print
<item>' .$P[0]. '</item>
<item>' .$P[1]. '</item>
<item>' .$P[2]. '</item>

any idea?
0

#4 User is online   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,902
  • Joined: 04-October 08
  • Location:DC, USA

Posted 12 April 2010 - 03:36 PM

queryColumn()
0

#5 User is offline   neel 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 133
  • Joined: 08-October 09

Posted 12 April 2010 - 05:12 PM

View Postqiang, on 12 April 2010 - 03:36 PM, said:

queryColumn()

Would you plz explain how to use this?
0

#6 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 12 April 2010 - 05:43 PM

Excerpt from the guide:

Quote

$rows=$command->queryAll(); // query and return all rows of result
$row=$command->queryRow(); // query and return the first row of result
$column=$command->queryColumn(); // query and return the first column of result

http://www.yiiframew...de/database.dao

HTH
/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#7 User is offline   neel 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 133
  • Joined: 08-October 09

Posted 13 April 2010 - 06:48 AM

View Posttri, on 12 April 2010 - 05:43 PM, said:

Excerpt from the guide:

http://www.yiiframew...de/database.dao

HTH
/Tommy


Thanx, Problem solved.
$column=$command->queryColumn();
:)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users