从mysqli_multi_query中检索数组。数组空?

时间:2015-01-25 08:56:59

标签: php android mysql

基本上我已经困住了几天我的异步问题了。 这是:我试图从我的数据库(本地使用phpmyadmin / mysql)检索当前用户的朋友的名字(由他的ID标识)。 我成功地使用这个SQL命令(使用phpmyadmin): 从users.username = $ username1;的用户中选择@userid:= users.uid;         从users.uid = any的用户中选择users.username(从其中userID = @ userid的朋友中选择friends.friendID);

当我尝试将其添加到我的Android应用程序时,会出现问题。 它说数组是空的。我确定问题出在php代码中,而不是在适配器或其他地方,因为我非常成功地使用了sql的适配器。 我不明白为什么因为我在phpmyadmin上运行sql时获得了良好的数据。我用mysqli_next_result()和mysqli_store_result()尝试了很多东西,现在调整了几天......

这是现在的PHP代码: 的functions.php

public function getAllFriends($username)
    {
        $username1 = "\"".$username."\"";
        $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
        $result = mysqli_multi_query($con, "select @userid := users.uid from users where users.username = $username1;
        select users.username from users where users.uid = any (select friends.friendID from friends where userID = @userid)");

        while (mysqli_next_result($con)) {;} // flush multi_queries
        mysqli_query($con, " select @userid := users.uid from users where users.username = $username1; "); 
        mysqli_query($con, "select users.username from users where users.uid = any (select friends.friendID from friends where userID = @userid)");

        $no_of_rows = mysqli_num_rows($result);
        if ($no_of_rows > 0) {
            //$result = mysqli_fetch_array($result);
            return $result;
        } else {
            // user not found
            return false;
        }
    }

的index.php:

else if ($tag == 'get_all_friends')
    {
        $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
        $uname = $_POST['username'];
        $user = $db->getAllFriends($uname);
        //$user = "ok";
        $rows = array();

        if ($user) 
        {
            while ($row = mysqli_fetch_array($user, MYSQLI_ASSOC))
            {
                $rows[] = $row;
            }
            //$response["success"] = 1;
            //$response["user"]["uname"] = $user["username"];
            mysqli_close($con);
            //echo json_encode($response);
            echo json_encode($rows);
        } 
        else
        {
            // user failed to store
            $response["error"] = 1;
            $response["error_msg"] = "GET ALL FRIENDS ERROR";
            echo json_encode($rows);
        }
    }

这是日志:

01-25 09:33:26.555: W/dalvikvm(12424): threadid=13: thread exiting with uncaught exception (group=0x413f6378)
01-25 09:33:26.655: E/AndroidRuntime(12424): FATAL EXCEPTION: AsyncTask #3
01-25 09:33:26.655: E/AndroidRuntime(12424): java.lang.RuntimeException: An error occured while executing doInBackground()
01-25 09:33:26.655: E/AndroidRuntime(12424):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-25 09:33:26.655: E/AndroidRuntime(12424):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-25 09:33:26.655: E/AndroidRuntime(12424):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-25 09:33:26.655: E/AndroidRuntime(12424):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-25 09:33:26.655: E/AndroidRuntime(12424):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-25 09:33:26.655: E/AndroidRuntime(12424):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-25 09:33:26.655: E/AndroidRuntime(12424):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-25 09:33:26.655: E/AndroidRuntime(12424):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-25 09:33:26.655: E/AndroidRuntime(12424):    at java.lang.Thread.run(Thread.java:856)
01-25 09:33:26.655: E/AndroidRuntime(12424): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
01-25 09:33:26.655: E/AndroidRuntime(12424):    at com.simple.async.AsyncGetAllFriends.doInBackground(AsyncGetAllFriends.java:22)
01-25 09:33:26.655: E/AndroidRuntime(12424):    at com.simple.async.AsyncGetAllFriends.doInBackground(AsyncGetAllFriends.java:1)
01-25 09:33:26.655: E/AndroidRuntime(12424):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-25 09:33:26.655: E/AndroidRuntime(12424):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-25 09:33:26.655: E/AndroidRuntime(12424):    ... 5 more
你知道吗? :

01-25 09:33:26.655: E/AndroidRuntime(12424): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

感谢帮助人员!

1 个答案:

答案 0 :(得分:0)

我发现了问题。 我用错误的参数调用我的AsyncTask。 对于php,我放弃了使用mysqli_multi_query,而是将查询分为两部分。 首先使用AsyncTask查找当前用户的id。 然后使用id调用GetAllFriends AsyncTask。 工作正常:))