2015-02-11 25 views
0

我想在整個表中搜索一個單詞。 所以,如果你搜索阿姆,你必須得到一切與阿姆單詞。從數組中創建一個對象PHP

我搜索

<?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;} 

它的工作原理,但不是究竟如何我想它。 結果是這樣的:

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))) 

沒關係,但我要的是拿出變量並使用它。

是否有一種方法可以將變量從數組中取出並使用它? 如果你們想了解更多關於我的代碼的信息,請詢問!

+0

你想訪問喜歡的對象或結果? – AnotherGuy 2015-02-11 12:14:00

+0

是的,如果這可以幫助我將變量從數組中取出 – 2015-02-11 12:17:37

+0

您是否擁有表格「專輯」的模型? – topher 2015-02-11 12:19:02

回答

2

嘗試使用此

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

這會給你的對象與列名數組作爲性能

如: -

foreach($result as $row) 
{ 
echo $row->albumcode; 
} 
0

唔只是做

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']; 

}

又名,基本的PHP

並請爲您的應用程序的安全性,學習如何做準備好的發言中警予

方式你的查詢現在我可以擦你的整個數據庫

+0

是的,謝謝,但它是爲了學校,所以我永遠不會把它放在網上或其他東西。如果我使用你的解決方案,我的本地主機崩潰。 – 2015-02-11 12:35:09

+0

呃,即使我沒有出錯:P嘗試修改後的代碼;-) – Tschallacka 2015-02-11 13:12:23

1

如果喲你想訪問結果集就像一個對象,你可以使用本地PHP類ArrayObject並提供標誌來表明。

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

您現在可以訪問結果如下所示:

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

希望這可以指導您,編碼快樂!

+0

感謝您的回覆,但如果我這樣做,我會收到一個錯誤,說undefined索引 – 2015-02-11 12:25:28

+0

我不明白爲什麼會出現錯誤。請確保根據您的數據庫列拼寫變量名稱。如果他們是,我很無能,對不起。 – AnotherGuy 2015-02-11 12:31:10

+1

數組可以有像$ array [「My Name」]和$ array [3]這樣的索引,對象字段遵循特定的命名約定。例如$ my_object-> 3是無效的。 – crafter 2015-02-13 06:05:20