2014-10-08 157 views
0

我做了我的SQL請求,這樣在我model.php:顯示結果SELECT *

public function getUserForAdmin() { 
    $arrayUser = $this->bdd->getArray('SELECT prenom AS prenomUFA, nom AS nomUFA, email AS emailUFA, cp AS cpUFA FROM user'); 
    return $arrayUser['prenomUFA']; 
} 

在我的index.php我這樣稱呼它

$userForAdmin = $user->getUserForAdmin(); 
echo $userForAdmin['prenomUFA']; 

有了這個,我剛纔的我prenom領域的第一個字母導致...

我陣列的輸出:

Array ( 
    [0] => Array ([prenom] => Coline [nom] => [email] => hj [userForAdmin] => 69570) 
    [1] => Array ([prenom] => Coline [nom] => [email] => [email protected] [userForAdmin] => 69570) 
    [2] => Array ([prenom] => Coline [nom] => [email] => [email protected] [userForAdmin] => 69570) 
    [3] => Array ([prenom] => Coline [nom] => [email] => hjkjk [userForAdmin] => 69570) 
    [4] => Array ([prenom] => Coline [nom] => [email] => [email protected] [userForAdmin] =>) 
    [5] => Array ([prenom] => Coline [nom] => [email] => hjkhjk [userForAdmin] => 69570) 
    [6] => Array ([prenom] => Coline [nom] => jkljl [email] => [email protected] [userForAdmin] =>) 
    [7] => Array ([prenom] => Coline [nom] => fgdfg [email] => [email protected] [userForAdmin] =>) 
    [8] => Array ([prenom] => Coline [nom] => ghjg [email] => [email protected] [userForAdmin] => ghj) 
    [9] => Array ([prenom] => ghj [nom] => ghjghj [email] => [email protected] [userForAdmin] =>) 
    [10] => Array ([prenom] => hjkhk [nom] => hjkhjk [email] => [email protected] [userForAdmin] =>) 
    [11] => Array ([prenom] => cvbcb [nom] => cvbcvb [email] => [email protected] [userForAdmin] =>) 
    [12] => Array ([prenom] => jhk [nom] => hjkhjk [email] => [email protected] [userForAdmin] =>) 
    [13] => Array ([prenom] => Coline [nom] => [email] => cghjghj [userForAdmin] => 45645) 
    [14] => Array ([prenom] => Coline [nom] => [email] => ghj [userForAdmin] => 44545) 
) 
+0

$ arrayUser是數組,但$ arrayUser ['userForAdmin']不是數組。它是數組中'userForAdmin'鍵的值。所以echo'

'。$ userForAdmin。'

';對我來說似乎很好。 – Ashish 2014-10-08 13:16:35

+0

@colinec請你詳細說明你想輸出的內容。你的代碼說它從數據庫表中取出名爲'user'的行。然後,您將結果集的第一行存儲在變量$ arrayUser中。在下一個語句中,您將使用$ arrayUser ['userForAdmin']返回存儲在'cp'列中的值(您在查詢中給出'userForAdmin'的別名)。 – Ashish 2014-10-08 13:30:02

+0

對不起,我想輸出我的所有字段,從我的SELECT中詢問。像:prenom,nom,email和cp。我意識到我的AS只是在推崇我的CP。如果我把所有的領域都放在AS中,那麼如何將它們歸還給它? – colinec 2014-10-08 13:33:02

回答

1
public function getUserForAdmin() { 
    $arrayUser = $this->bdd->getArray('SELECT prenom AS prenomUFA, nom AS nomUFA, email AS emailUFA, cp AS cpUFA FROM user'); 
    return $arrayUser; 
} 

的index.php中這樣稱呼它 -

$userForAdmin = $user->getUserForAdmin(); 
foreach($userForAdmin as $rows) 
{ 
    echo '<p>'.$rows['prenomUFA'].'</p><br>'; 
    echo '<p>'.$rows['nomUFA'].'</p><br>'; 
    echo '<p>'.$rows['emailUFA'].'</p><br>'; 
    echo '<p>'.$rows['cpUFA'].'</p><br>'; 
} 

以上代碼輸出所有的值。

+0

就是這樣。非常感謝你。 – colinec 2014-10-08 14:28:19

+0

我現在明白了。 – colinec 2014-10-08 14:29:57

2

爲了使整個陣列從函數返回做到這一點 -

public function getUserForAdmin() { 
    $arrayUser = $this->bdd->getArray('SELECT prenom, nom, email, cp AS userForAdmin FROM user'); 
    return $arrayUser; 
} 

調用它像這樣 -

$userForAdmin = $user->getUserForAdmin(); 
print_r($userForAdmin); // testing to see what is in the array 

看來,getArray()返回數組的數組和你的AS別名被正確地返回。要訪問你必須這樣做,這樣的單個項目:

echo $userForAdmin[0]['userForAdmin']; // note the specification used in multidimensional arrays 
echo $userForAdmin[0]['prenom']; // has no 'AS' alias 
echo $userForAdmin[0]['nom']; 
echo $userForAdmin[0]['email']; 

你可能想看看getArray(),以便它返回的東西更方便的工作。

+0

其實我的userForAdmin只需要我的「CP」字段。所以當我嘗試print_r($ userForAdmin)顯示所有字段時,它只顯示「cp」 – colinec 2014-10-08 13:20:33

+0

那麼@colinec有什麼問題? – 2014-10-08 13:23:10

+0

如果我把所有字段放在SELECT prenom AS prenomUser,nom AS nomUser之後的所有字段中...如何告訴我的回報我希望他返回我所有的字段? – colinec 2014-10-08 13:26:13