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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题