問題描述
從數據庫返回多個值 (return multiple values from database)
我正在嘗試從數據庫中回顯兩個值,並希望在 html 中的不同位置回顯該值,但是當我在 html 中調用該函數時,它在同一個地方給了我兩個值,但我希望在不同的地方該值如何請幫忙。
HTML
<div id="container">
<div id="header">
<a href="home.php?q=logout">LOGOUT</a>
</div>
<div id="main‑body">
<br/><br/><br/><br/>
<h1>
Hello <?php $user‑>get_fullname($uid); ?>
<?php echo $uid; ?>
</h1>
</div>
<div id="footer"></div>
</div>
類名用戶中的php函數
public function get_fullname($uid){
$sql3="SELECT fullname,uemail FROM users WHERE uid = $uid";
$result = mysqli_query($this‑>db,$sql3);
$user_data = mysqli_fetch_array($result);
echo $user_data['fullname'];
echo $user_data['uemail'];
}
參考解法
方法 1:
public function get_fullname($uid){
$sql="SELECT fullname,uemail FROM users WHERE uid = $uid";
$result = mysqli_query($this‑>db,$sql);
$user_data = mysqli_fetch_array($result);
return (object)array('username'=>$user_data['fullname'],'email'=>$user_data['uemail']);
}
/* Call the class method ‑ doesn't echo back to screen */
$userdata=$user‑>get_fullname($uid);
Then, later in the code:
echo $userdata‑>username;
/* or */
echo $userdata‑>email
One thing however, perhaps you ought to investigate prepared statements
for use with mysqli
(by Sheraz、Professor Abronsius)