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 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)