0

I am trying to search one word in my whole table. So if you search Eminem, you have to get everything with the word Eminem.

I search

<?php

 $sql="SELECT * FROM album WHERE albumartiest like '$zoek'";
    $resultaatcolumn = Yii::app()->db->CreateCommand($sql)->queryAll();                         
    if($resultaatcolumn != null){
     $zoekresultaat[] = $resultaatcolumn;}
 $sql="select * from album where albumnaam like '%$zoek%'";
    $resultaatcolumn = Yii::app()->db->CreateCommand($sql)->queryAll();
    if($resultaatcolumn != null){
     $zoekresultaat[] = $resultaatcolumn;}
 $sql="select * from album where albumartiest like '%$zoek%'";
    $resultaatcolumn = Yii::app()->db->CreateCommand($sql)->queryAll();
    if($resultaatcolumn != null){
     $zoekresultaat[] = $resultaatcolumn;}
 $sql="select * from album where albumgenre like '%$zoek%'";
    $resultaatcolumn = Yii::app()->db->CreateCommand($sql)->queryAll();
    if($resultaatcolumn != null){
     $zoekresultaat[] = $resultaatcolumn;}
 $sql="select * from album where albumafspeelijst like '%$zoek%'";
    $resultaatcolumn = Yii::app()->db->CreateCommand($sql)->queryAll();
    if($resultaatcolumn != null){
     $zoekresultaat[] = $resultaatcolumn;}

It works, but not exactly how I want it. The result is this:

Array ( [0] => Array ( [0] => Array ( [albumcode] => 45 [albumnaam] => recovery [albumafspeelijst] => ["Cold Wind Blows","Talkin' 2 Myself","On Fire","Won't Back Down","W.T.P.","Going Through Changes","Not Afraid","Seduction","No Love","Space Bound","Cinderella Man","To Life","So Bad","Almost Famous","Love The Way You Lie","You're Never Over",""] [albumartiest] => Eminem [albumgenre] => hip-hop [albumimage] => images\eminemrecovery.png [albumprijs] => 20 ) ) [1] => Array ( [0] => Array ( [albumcode] => 45 [albumnaam] => recovery [albumafspeelijst] => ["Cold Wind Blows","Talkin' 2 Myself","On Fire","Won't Back Down","W.T.P.","Going Through Changes","Not Afraid","Seduction","No Love","Space Bound","Cinderella Man","To Life","So Bad","Almost Famous","Love The Way You Lie","You're Never Over",""] [albumartiest] => Eminem [albumgenre] => hip-hop [albumimage] => images\eminemrecovery.png [albumprijs] => 20 ) ) )

that's okay, but what I want is take out variable's and use it.

is there a way that I can get variable's out of the array and use it? If you guys want more information about my code please ask!

4
  • Do you want to access the result like an object or? Commented Feb 11, 2015 at 12:14
  • yes, if that helps me to get the variables out of the array Commented Feb 11, 2015 at 12:17
  • Do you have a model for the table album? Commented Feb 11, 2015 at 12:19
  • You may try like, (object)$zoekresultaat Commented Feb 11, 2015 at 13:17

3 Answers 3

2

Try using this

Yii::app()->db->CreateCommand($sql)->setFetchMode(PDO::FETCH_OBJ)->queryAll()

This will give you an array of objects with column name as the properties.

Eg:-

foreach($result as $row)
{
echo $row->albumcode;
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to access the result set like an object you can use the native PHP class ArrayObject and provide the flag to indicate that.

$album = new ArrayObject($result, ArrayObject::ARRAY_AS_PROPS);

You can now access the results like the following:

$code = $album->albumcode;
$name = $album->albumnaam;

Hope this can guide you, happy coding!

3 Comments

thanks for your reply, But if I do this, I get an error that says undefined index
I do not understand why you get an error. Please make sure the variable name is spelled according to your database columns. If they are, I am clueless, sorry.
Arrays can have indexes like $array["My Name"] and $array[3],, object fields follow specific naming conventions. For example $my_object->3 is invalid.
0

uhhh just do

foreach($zoekresultaat as $key => $value) {
   //do what I want with each seperate returened result. The array key is in $key and the result array is in $value
   echo $value['albumcode'] . ' = '. $value['albumnaam'];

}

aka, basic php

And please for the security of your app, learn how to do prepared statements in yii

The way your query is now I could wipe your entire database

2 Comments

Yes thanks, but it is for school so I would never put it online or something. If I use your solution my localhost crashes.
Ugh, even I am not excempt from errors :P try the modified code ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.