php - 如何使用 php web 服务从 MySql 中的 2 个不同表中获取数据

标签 php mysql web-services

我有一个关于摄影的项目,我通过此网络服务收到通知;

<?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'],

我知道问题是我正在使用 $row 但我不知道如何在另一个表“帖子”中获取此数据。我在“通知”和“帖子”中有一个通用键。在帖子中,这个列名是“id”,在“通知”表中,这个列名是“postId”。所以不久我想使用此 Web 服务将 imgUrl 参数放入“帖子”表中。 here 我怎样才能做到这一点?谢谢。

最佳答案

使用左连接或内连接,取决于您的情况。

$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 时返回一行。

关于php - 如何使用 php web 服务从 MySql 中的 2 个不同表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37444058/

相关文章:

php - 在没有日历表的情况下搜索给定范围内的可用日期

php - Codeigniter Right Join with Where 条件未给出结果

PHP 循环取模

mysql - 为什么重装安装文件时Mysql服务器配置没有配置服务器密码?

MySQL '@' : what does it do?

javascript - 不点击提交按钮的下拉选择

php - PHP 中的 MySQL 删除查询在 IF 语句中不起作用

ios - 在 OSX 中创建 RESTful Web 服务(java/python/?)以使用 swift 测试 ios 应用程序的简单方法

web-services - IBM Liberty SSL 握手失败

web-services - 如何将 Web 服务响应保存到我从中提取数据的同一个 Excel 工作表?