我有以下功能:
function getUser($user_id){
$mysqli = dbConnect();
$gu = "select * from users where user_id = '$user_id'";
$ru = $mysqli->query($gu);
$user = $ru->fetch_array();
return $user;
}
这被称为例如:
$user_id = $_SESSION[user_id];
getUser($user_id);
然后我想简单地回显我想要的字段,例如名称。但是,当我尝试以下操作时,它返回空
echo "users name is $user['name']"; // returns: users name is
有一个更好的方法吗?
更新还尝试了以下但仍然是空的:
function getUser($user_id){
$mysqli = dbConnect();
$gu = "select * from users where user_id = '$user_id'";
$ru = $mysqli->query($gu);
$user = array();
while($row = $ru->fetch_array()) {
$user[] = $row;
}
return $user;
}