显示结果选择*

时间:2014-10-08 12:48:05

标签: php sql codeigniter

我在我的model.php中完成了我的SQL请求:

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] => officiel@coucou.fr [userForAdmin] => 69570 ) 
    [2] => Array ( [prenom] => Coline [nom] => [email] => officiel@coucou.fr [userForAdmin] => 69570 ) 
    [3] => Array ( [prenom] => Coline [nom] => [email] => hjkjk [userForAdmin] => 69570 )
    [4] => Array ( [prenom] => Coline [nom] => [email] => officiel@coucou.fr [userForAdmin] => ) 
    [5] => Array ( [prenom] => Coline [nom] => [email] => hjkhjk [userForAdmin] => 69570 )   
    [6] => Array ( [prenom] => Coline [nom] => jkljl [email] => jkljl@fgfg.fr [userForAdmin] => ) 
    [7] => Array ( [prenom] => Coline [nom] => fgdfg [email] => dfgfdg@dsdfg.hfg [userForAdmin] => ) 
    [8] => Array ( [prenom] => Coline [nom] => ghjg [email] => ghj@fddfg.fr [userForAdmin] => ghj ) 
    [9] => Array ( [prenom] => ghj [nom] => ghjghj [email] => ghgh@dfgdfg.fra [userForAdmin] => ) 
    [10] => Array ( [prenom] => hjkhk [nom] => hjkhjk [email] => hjk@sdfdf.fr [userForAdmin] => ) 
    [11] => Array ( [prenom] => cvbcb [nom] => cvbcvb [email] => cvbcv@dfgdfg.fr [userForAdmin] => ) 
    [12] => Array ( [prenom] => jhk [nom] => hjkhjk [email] => hjhjk@dfgdfg.fr [userForAdmin] => ) 
    [13] => Array ( [prenom] => Coline [nom] => [email] => cghjghj [userForAdmin] => 45645 )   
    [14] => Array ( [prenom] => Coline [nom] => [email] => ghj [userForAdmin] => 44545 ) 
)

2 个答案:

答案 0 :(得分: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(),以便返回更方便使用的内容。

答案 1 :(得分: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>';
}

以上代码输出所有值。