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?
Page 1 of 1
Array problem
#2
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:
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);
#3
Posted 12 April 2010 - 01:53 PM
Y!!, 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:
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?
#6
Posted 12 April 2010 - 05:43 PM
Excerpt from the guide:
http://www.yiiframew...de/database.dao
HTH
/Tommy
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
$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
#7
Posted 13 April 2010 - 06:48 AM
Share this topic:
Page 1 of 1