如何按日期从存储在数组中的表中获取所有用户?

时间:2012-01-30 06:46:20

标签: php mysql

所以我有这个函数从MySql表中获取所有用户并将它们存储在一个数组中:

public function graphVals() {
    $sql = "SELECT user_username FROM users WHERE user_register_date > CURDATE() - 2592000";

    if ($stmt = $this->connect->prepare($sql)) {    
        $stmt->execute();
        $stmt->bind_result($username);
        while ($row = $stmt->fetch()) {
            $stmt->bind_result($username);
            $data[] = $username;
        }
        $stmt->close();
        foreach($data as $key) {

            $length[] = sizeof($key);

        }
        print_r($data);
    } else {
        $error              = true;
        $message['error']   = true;
        $message['message'] = CANNOT_PREPARE_DATABASE_CONNECTION_MESSAGE;
        return json_encode($message);
    }   
}

之后,如您所见,我想返回一个包含usernames数组长度的数组。

但我的问题是,正如你所看到的那样,我正在获取一个包含所有用户名的数组,每个用户名都在一个索引处,我实际想要做的是将所有用户注册在一个日期并将它们存储在一个索引中,所有用户在上一个用户之后的日期注册并将其存储在下一个索引等中

我该怎么做?我可以调整此功能吗?

3 个答案:

答案 0 :(得分:1)

首先在查询中添加ORDER BY。这不是强制性的,但会为您简化

SELECT user_username FROM users
WHERE user_register_date > CURDATE() - 2592000
ORDER BY user_register_date

然后检查循环迭代中的日期,如下所示:

while ($row = $stmt->fetch()) {
      $stmt->bind_result($username);
      $stmt->bind_result($date);

      if (!isset($data[$date])) {
           // first time we encounter this date, creating a new array for it
           $data[$date] = array();
      }          
      $data[$date][] = $username;
}

答案 1 :(得分:1)

这很容易做到:

public function graphVals() {
    $sql = "SELECT user_username FROM users WHERE user_register_date > CURDATE() - 2592000 ORDER BY user_register_date ASC";

    $data = array();
    if ($stmt = $this->connect->prepare($sql)) {    
        $stmt->execute();
        $stmt->bind_result($username);
        $stmt->bind_result($date);
        while ($row = $stmt->fetch()) {
            $stmt->bind_result($username);
            if (!isset($data[$date])) {
            // when the date is the first time in the array. Add that date as index for the array   
                 $data[$date] = array();
            }
            $data[$date][] = $username;
        }
        $stmt->close();
        foreach($data as $key) {

            $length[] = sizeof($key);

       }
       print_r($data);
    } else {
          $error              = true;
          $message['error']   = true;
          $message['message'] = CANNOT_PREPARE_DATABASE_CONNECTION_MESSAGE;
          return json_encode($message);
    }   
 }

希望这会有所帮助:)

答案 2 :(得分:0)

所以这就是我看到你的示例代码后想出来的。现在我按日期得到它,作为索引的日期,唯一的是我在NOW()之前而不是CURDATE()注册时,我得到小时,分钟和秒。但我认为这是解决方案。让我知道你的想法,如果我应该做任何其他事情。

$sql = "SELECT user_username, user_register_date FROM users WHERE user_register_date > CURDATE() - 2592000 ORDER BY user_register_date";

    if ($stmt = $this->connect->prepare($sql)) {    
        $stmt->execute();
        $stmt->bind_result($username,$date);
        while ($row = $stmt->fetch()) {
            $stmt->bind_result($username, $date);
            if (!isset($data[$date])) {
                 $data[$date] = array();
            }
            $data[$date][] = $username;
        }
        $stmt->close();
        foreach($data as $key) {

            $length[] = sizeof($key);

       }
       print_r($data);
    } else {
        $error              = true;
        $message['error']   = true;
        $message['message'] = CANNOT_PREPARE_DATABASE_CONNECTION_MESSAGE;
        return json_encode($message);
    }