所以我试图在我的站点上的多个位置获取(PDO)函数中的数据,并且不想在函数中回显行。似乎无法使其工作。
我的功能
function getUser(){
global $db;
$userId = $_SESSION['userId'];
$sql = "SELECT firstName FROM users WHERE userId = $userId";
$stmt = $db->prepare($sql);
$stmt->execute();
$row = $stmt->fetch();
//the problem
echo $row['firstName'];
}
我如何调用函数
<?php
getUser();
?>
我不想在函数中回声$row["firstname"],但在我想使用它的网站上?
希望它有意义?
感谢
不是回声:
//the problem
echo $row['firstName'];
只需退回
return $row['firstName'];
然后当你叫它时,你可以这样做:
<?php
$user = getUser();
echo "$user<br />'n";
?>
如果你需要返回多个列,你可以这样做:
return array($row['col1'], $row['col2']);
然后像这样在另一端分配:
list($col1, $col2) = getXyz($someID);