dongsun2789 2015-03-04 18:41 采纳率: 100%
浏览 55
已采纳

将获取的查询存储到全局数组PHP中

I'm trying to create a global variable which contains user specific data from my database. I've been looking over the internet but I couldn't find the right answer. I'm not quite sure how to approach this.

At this point of code, the user has been registered and logged in and his login data is saved inside $s_email and $s_password, which are session variables.

Here's some code (mysql.php) which contains the mysql class:

class mysql {

// Create database connection
private $db;
function __construct() {
    $this->db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME) or 
                die('Database fout.');
}

// Receive user data based on cookie, then fetch this data into an array
function getUserData($s_email, $s_password, $data){

    $sql = "SELECT 
                `id`, 
                `firstname`, 
                `lastname`, 
                `city`,
                `country`,
                `gender`,
                `bio`,
                `active`,
                `member_since`
            FROM `users` 
            WHERE email = '$s_email' 
            AND password ='$s_password'"; 

    // perform the query and store the result
    $result = $this->db->query($sql);

    // if the $result contains at least one row
    if ($result->num_rows > 0) {

      // output data of each row from $result
        $data = array();
        global $data;

        while($row = $result->fetch_assoc()) {
            $data[] = $row;
        } 
    }
}

}

And this is my profile.php which will try to run the function (the echo $data[''] does not work, but that's the kind of approach I would like to take on this system):

    <?php
        // Receive user data and send it to mysql class
        $mysql->getUserData($s_email, $s_password, $data);
        echo $data['email'];
    ?>

Eventually I'm trying to create a simple access method to the array variables.

NOTE: I've only been scripting PHP and MySQL for 2 days, so any advice on my code is really appreciated.

Thank you,

  • 写回答

2条回答 默认 最新

  • douyun8901 2015-03-04 19:03
    关注

    Don't use globals.

    In your function (don't use $data as an argument):

    function getUserData($s_email, $s_password) {
        // code
    
        while($row = $result->fetch_assoc()) {
            $data[] = $row;
        }
        return $data;
    }
    

    Then to use it (don't pass $data as an argument):

    $data = $mysql->getUserData($s_email, $s_password);
    

    Now you have rows so loop over them:

    foreach($data as $row) {
        echo $row['email'];
    }
    

    And you need to select email in the query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决