從數據庫返回多個值 (return multiple values from database)


問題描述

從數據庫返回多個值 (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 SherazProfessor Abronsius)

參考文件

  1. return multiple values from database (CC BY‑SA 2.5/3.0/4.0)

#php-5.5 #PHP #php-5.4






相關問題

當作為參數傳遞時,PHP 如何解釋和評估函數和匿名函數? (How does PHP interpret and evaluate function and anonymous function when passed as an argument?)

使用 symfony 時 PHP 5.5 無法識別服務器 (PHP 5.5 won't recognise server when using symfony)

升級到 PHP5.5 時 Wordpress 崩潰 (Wordpress crashes when upgrading to PHP5.5)

從數據庫返回多個值 (return multiple values from database)

PHP:嵌套 foreach 循環的問題 (PHP: Issue with nested foreach loops)

如果條件在幾小時、幾天內不起作用 (if condition not working for Hours,Days)

Slim 3.3 輸出中缺少字符 (Slim 3.3 missing characters in output)

PHP排序對象值但保留原始相對順序 (PHP sort objects value but retain original relative order)

遇到錯誤無法加載請求的文件:helpers/form_validation_helper.php (An Error Was Encountered Unable to load the requested file: helpers/form_validation_helper.php)

如何從類的函數內部訪問全局變量 (How to access global variable from inside class's function)

如何修改輸出緩衝區? (How to modify output buffer?)

在同一台 Ubuntu 服務器上安裝和配置 PHP5 和 7 (Install and configure PHP5 and 7 on same Ubuntu server)







留言討論