2016-05-25 14 views
1

私は写真に関するプロジェクトを持っており、私はこのWebサービスで通知を受けています。php webserviceを使用してMySqlの2つの異なるテーブルからデータを取得する方法

<?php 


class notify extends db_connect 
{ 
    private $requestFrom = 0; 
    private $language = 'en'; 
public function __construct($dbo = NULL) 
{ 

    parent::__construct($dbo); 
} 

private function getMaxId() 
{ 
    $stmt = $this->db->prepare("SELECT MAX(id) FROM notifications"); 
    $stmt->execute(); 

    return $number_of_rows = $stmt->fetchColumn(); 
} 

public function getAll($notifyId = 0) 
{ 

    if ($notifyId == 0) { 

     $notifyId = $this->getMaxId(); 
     $notifyId++; 
    } 

    $notifications = array("error" => false, 
          "error_code" => ERROR_SUCCESS, 
          "notifyId" => $notifyId, 
          "notifications" => array()); 

    $stmt = $this->db->prepare("SELECT * FROM notifications WHERE notifyToId = (:notifyToId) AND id < (:notifyId) ORDER BY id DESC LIMIT 20"); 
    $stmt->bindParam(':notifyToId', $this->requestFrom, PDO::PARAM_INT); 
    $stmt->bindParam(':notifyId', $notifyId, PDO::PARAM_INT); 


    if ($stmt->execute()) { 

     if ($stmt->rowCount() > 0) { 

      while ($row = $stmt->fetch()) { 

       $time = new language($this->db, $this->language); 

       if ($row['notifyFromId'] == 0) { 

        $profileInfo = array("id" => 0, 
             "state" => 0, 
             "username" => "", 
             "fullname" => "", 
             "lowPhotoUrl" => "/img/profile_default_photo.png"); 

       } else { 

        $profile = new profile($this->db, $row['notifyFromId']); 
        $profileInfo = $profile->get(); 
        unset($profile); 
       } 

       $data = array("id" => $row['id'], 
           "type" => $row['notifyType'], 
           "postId" => $row['postId'], 
           "fromUserId" => $profileInfo['id'], 
           "fromUserState" => $profileInfo['state'], 
           "fromUserUsername" => $profileInfo['username'], 
           "fromUserFullname" => $profileInfo['fullname'], 
           "fromUserPhotoUrl" => $profileInfo['lowPhotoUrl'], 
           "createAt" => $row['createAt'], 
           "imgUrl" => $row['imgUrl'], 
           "timeAgo" => $time->timeAgo($row['createAt'])); 

       array_push($notifications['notifications'], $data); 

       $notifications['notifyId'] = $row['id']; 

       unset($data); 
      } 
     } 
    } 

    return $notifications; 
} 

public function createNotify($notifyToId, $notifyFromId, $notifyType, $postId = 0) 
{ 
    $createAt = time(); 

    $stmt = $this->db->prepare("INSERT INTO notifications (notifyToId, notifyFromId, notifyType, postId, createAt ,imgUrl) value (:notifyToId, :notifyFromId, :notifyType, :postId, :createAt, :imgUrl)"); 
    $stmt->bindParam(":notifyToId", $notifyToId, PDO::PARAM_INT); 
    $stmt->bindParam(":notifyFromId", $notifyFromId, PDO::PARAM_INT); 
    $stmt->bindParam(":notifyType", $notifyType, PDO::PARAM_INT); 
    $stmt->bindParam(":imgUrl", $postImage, PDO::PARAM_STR); 
    $stmt->bindParam(":postId", $postId, PDO::PARAM_INT); 
    $stmt->bindParam(":createAt", $createAt, PDO::PARAM_INT); 
    $stmt->execute(); 

    return $this->db->lastInsertId(); 
} 

public function remove($notifyId) 
{ 
    $stmt = $this->db->prepare("DELETE FROM notifications WHERE id = (:notifyId)"); 
    $stmt->bindParam(":notifyId", $notifyId, PDO::PARAM_INT); 
    $stmt->execute(); 
} 

public function removeNotify($notifyToId, $notifyFromId, $notifyType, $postId = 0) 
{ 
    $stmt = $this->db->prepare("DELETE FROM notifications WHERE notifyToId = (:notifyToId) AND notifyFromId = (:notifyFromId) AND notifyType = (:notifyType) AND postId = (:postId)"); 
    $stmt->bindParam(":notifyToId", $notifyToId, PDO::PARAM_INT); 
    $stmt->bindParam(":notifyFromId", $notifyFromId, PDO::PARAM_INT); 
    $stmt->bindParam(":notifyType", $notifyType, PDO::PARAM_INT); 
    $stmt->bindParam(":postId", $postId, PDO::PARAM_INT); 
    $stmt->execute(); 
} 

public function getAllCount() 
{ 
    $stmt = $this->db->prepare("SELECT count(*) FROM notifications WHERE notifyToId = (:notifyToId)"); 
    $stmt->bindParam(":notifyToId", $this->requestFrom, PDO::PARAM_INT); 
    $stmt->execute(); 

    return $number_of_rows = $stmt->fetchColumn(); 
} 

public function getNewCount($lastNotifyView) 
{ 
    $stmt = $this->db->prepare("SELECT count(*) FROM notifications WHERE notifyToId = (:notifyToId) AND createAt > (:lastNotifyView)"); 
    $stmt->bindParam(":notifyToId", $this->requestFrom, PDO::PARAM_INT); 
    $stmt->bindParam(":lastNotifyView", $lastNotifyView, PDO::PARAM_INT); 
    $stmt->execute(); 

    return $number_of_rows = $stmt->fetchColumn(); 
} 

public function setLanguage($language) 
{ 
    $this->language = $language; 
} 

public function getLanguage() 
{ 
    return $this->language; 
} 

public function setRequestFrom($requestFrom) 
{ 

    $this->requestFrom = $requestFrom; 
} 

public function getRequestFrom() 
{ 

    return $this->requestFrom; 
} 

}

とこのコードの動作完璧しかし、私は名前がimgUrlで新しいパラメータを挿入していて、それが[通知]データベース内のデータを取得しています。

"imgUrl" => $row['imgUrl'], 

は、私はこの問題は、私は$行を使用していますが、私は別のテーブル「ポスト」でこのデータを取得する方法を知らないことを知っています。私は「通知」と「投稿」に共通のキーを持っています。ポストでは、このcoloumnの名前は 'id'で、 'notifications'の表ではこのcoloumnの名前は 'postId'です。だから私はすぐに私はimgUrlパラメータは、このWebサービスで '投稿'テーブルに取得したい。 here どうすればいいですか?ありがとうございました。

+0

ジョインについて読む。ここでは1つのクエリしか必要ありません。 – Strawberry

答えて

2

左結合または内部結合を使用するかどうかは、使用するシナリオによって異なります。

$stmt = $this->db->prepare("SELECT notifications.*, posts.imgUrl 
           FROM notifications 
         LEFT JOIN posts ON notifications.postId = posts.Id 
          WHERE notifyToId = (:notifyToId) 
           AND id < (:notifyId) 
          ORDER BY id DESC 
          LIMIT 20"); 

Left joinは常に、あなたが任意の投稿(postIdがnullの場合、またはしない)を持っていない場合でも、行が返されます。

Inner joinは、postId != nullの場合にのみ行を返します。

+0

完璧な答えをありがとう。しかし、私はmysqlとクエリで本当に新しいです。だから私は "imgUrl" => $ row ['imgUrl']、あなたの答えについてこのコードをどのように変更することができます。そして私の方法であなたの答えを実装することができます。ありがとうございます –

+0

私の答えで 'function getAll()'の元の 'select'を置き換えてください。その後、 '$ row'を' var_dump'した後、どのような結果が得られたのかを見てから、あなたのやり方を変えてください。 –

+0

OK、私は自分のコードを変更していますが、nullを与えていますので、notifyToId =(:notifyToId)とid <(:notifyId)の部分をnotifyToId = notifyToIdと変更してimgUrlを渡しますそれはすべての通知を与える。何が問題ですか? –

関連する問題