dsms21398
2016-10-25 14:17
浏览 47
已采纳

MYSQL查询和PHP - 查询返回nil到IOS App

I am calling a function from my IOS app which calls the function in PHP file which then should return the data retrieved, but it is return nil since I added a new part to my query.

My new query is working in the MYSQL tester service on my DB, but strangely is returning nil in my app.

Here is how I have it all set up in my PHP file:

function fetchAssocStatement($stmt)
    {
        if($stmt->num_rows>0)
        {
            $result = array();
            $md = $stmt->result_metadata();
            $params = array();
            while($field = $md->fetch_field()) {
                $params[] = &$result[$field->name];
            }
            call_user_func_array(array($stmt, 'bind_result'), $params);
            if($stmt->fetch())
                return $result;
        }

        return null;
    }

    // Select all posts + user information made by user with relevant $id
    public function selectPosts($id) {

        // declare array to store selected information
        $returnArray = array();

        /*
        // sql JOIN
        $sql = "SELECT Posts.id,
        Posts.uuid,
        Posts.caption,
        Posts.path,
        Posts.date,
        Posts.imageHeight,
        USERS.id,
        USERS.username,
        USERS.fullname,
        USERS.profileImage
        FROM Posts JOIN USERS ON
        Posts.id = $id AND USERS.id = $id ORDER by date DESC
        LIMIT 0, 5";
        */
        $sql = "SELECT Posts.id,
        Posts.uuid,
        Posts.caption,
        Posts.path,
        Posts.date,
        Posts.imageHeight,
        USERS.id,
        USERS.username,
        USERS.fullname,
        USERS.profileImage,
        coalesce(A.LikeCNT,0)
        FROM Posts INNER JOIN USERS ON 
        Posts.id = $id AND USERS.id = $id
        LEFT JOIN (SELECT COUNT(A.uuidPost) LikeCNT, A.UUIDPost
        FROM Activity A
        WHERE type = "" 
        GROUP BY A.UUIDPOST) A
        on A.UUIDPost = Posts.uuid
        ORDER BY date DESC
        LIMIT 0, 5";


      if($stmt = $this->connection->prepare($sql))
{
    $stmt->execute();
    $stmt->store_result();

    while($row = $this->fetchAssocStatement($stmt))
    {
        $returnArray[] = $row;
    }

    //$stmt->close();
}

        return $returnArray;


    }

As you'll be able to see I have a commented out query, which was my previous query for getting the posts(which worked great). But now that I am trying to get the "like count" for all the images, in my app it is return nothing...

If anyone can see one, please let me know!

Thanks in advance!!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douqufan9148 2016-10-25 14:33
    已采纳

    You need to trap for errors. I think the SQL being executed is throwing an error and you're not trapping it; which is why no records are being returned. The type = "" seems to be an error in my mind. Either you want type is null or type = '' (empty set) but not type = "" or maybe you want type = 'somevalue' or no value for type at all. (eliminate the where clause entirely) I'd start there (eliminate where clause) and if you get results then you know the problem is with the where clause

    已采纳该答案
    打赏 评论
  • drl47263 2016-10-25 14:39

    This:

    Posts.id = $id AND USERS.id = $id
    

    That's a totally wonky join condition. You want posts and users who happen to share the exact same ID, which is HIGHLY unlikely to ever occur.

    No idea on your actual table structure, but you probably want something more like

    posts.user_id = users.id
    ...
    where users.id = $id
    
    打赏 评论

相关推荐 更多相似问题