简体   繁体   中英

Retrieving array from mysqli_multi_query. array empty?

Basically I have been stuck for days now on a problem with my async. Here it is : I'm trying to retrieve from my database (local using phpmyadmin/mysql) the names of the friends of the current user (identified by his ID). Which I succeeded to do with this SQL command (working on phpmyadmin): 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);

The problem happens when I try to get it to my android app. It says that the array is empty. I'm sure the problem is in the php code and not in adapter or elsewhere, because I used adapter with sql very successfully. I don't understand why because I get the good data when I run the sql on phpmyadmin. I tried many things with mysqli_next_result() and mysqli_store_result() and tweaking for days now...

Here's the php code as it is right now : 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);
        }
    }

Here's the log :

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

You see that? :

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

Thanks for the help guys!

I found the problem. I was calling my AsyncTask with the wrong parameters. And for the php, I gave up using mysqli_multi_query, instead I divided the query in two. First find the id of the current user with an AsyncTask. Then call the GetAllFriends AsyncTask with the id. Works fine :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM