【问题标题】:how to perform 2 queries within same function using mysql db & slim 3 PHP framework如何使用 mysql db & slim 3 PHP 框架在同一函数中执行 2 个查询
【发布时间】:2017-06-06 00:10:05
【问题描述】:

我正在尝试检索在 ios 应用中被请求为好友的用户,并将它们返回到应用中的好友请求列表中。为此,我使用了一个 PHP 函数,该函数在通知表中查询发送它的 user_id,然后返回与特定通知类型匹配并标记为未读的所有行。然后我想查询我的用户表并将发送请求的用户返回到这个特定的 user_id。 这是我构建的代码:

function getSquadRequests($req, $res) {
   global $db;

   $user_id = validateUserAuthentication($req);

   if ($user_id) {
       $query = $db->prepare(
         'SELECT * 
          FROM tblNotification 
          WHERE notification_to_id   = :user_id 
            AND notification_type    = 3 
            AND notification_is_read = 0
         ');

       $query->bindParam(':user_id', $user_id);

       if ($query->execute()) {
           $user = $query->fetch(PDO::FETCH_NAMED);

           // if user exist
           if ($user) {
               $query = $db->prepare(
                   'select * from tblUser where user_id = :not_from_id'
               );
               $query->bindParam(':not_from_id', $user['notification_from_id']);

               if ($query->execute()) {
                   $data = $query->fetchAll(PDO::FETCH_ASSOC);
               }

               $newRes = makeResultResponseWithObject($res, 200, $data);

           }
       } else {
           $newRes = makeResultResponseWithString(
               $res, 400, $query->errorInfo()[2]
           );
   }

   return $newRes;
}

【问题讨论】:

    标签: php mysql slim-3


    【解决方案1】:

    如果我理解正确,那么你的问题只是一个 sql 问题,这个查询应该返回预期的用户:

    SELECT u.* 
    FROM tblUser AS u
        LEFT JOIN tblNotification AS n 
        ON u.user_id = n.notification_from_id
    WHERE n.notification_to_id    = :user_id
      AND n.notification_type     = 3 
      AND n.notification_is_read  = 0
      AND n.notification_from_id <> :not_from_id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      相关资源
      最近更新 更多