0

i get some confused with one function i create on one array i get the correct result array and with other function the return result is object.

With that function the result is array :

public static function find_user_by_id ($user_id)
    {
        $result = self::find_this_query("SELECT * from users WHERE id = $user_id  LIMIT 1");
        $found_user_query_result = mysqli_fetch_array($result);

        return $found_user_query_result;
    }

but when i create a new function like below :

    public static function find_user_by_id_new ($user_id)
    {

        $result_array = self::find_this_query("SELECT * from users WHERE id = $user_id  LIMIT 1");

        if (!empty($result_array)){
            $first_item = array_shift($result_array);
            return $first_item;
        }
        else{

            return false;
        }
    }

the return is object?

What i make different and not get the same result ?

  • The first one is calling `mysqli_fetch_array()`, which returns one row of results as an array. The second one doesn't call that. – Barmar Aug 28 '18 at 16:54
  • i know that but with array_shift i used it to remove the first element of the array, and returns the value of the removed element. – Christos Ploutarchou Aug 28 '18 at 16:57
  • Yeah, everything I've read says that if you use the `Traversable` interface to `mysqli_result` it should return associative arrays, not objects. I'm not sure why you're getting objects. – Barmar Aug 28 '18 at 16:58
  • [This question](https://stackoverflow.com/questions/13417944/php-mysqli-result-use-traversable-interface-with-fetch-object) asks how to make it return objects instead of arrays, and the answers are that you can't. So what you're seeing shouldn't happen. – Barmar Aug 28 '18 at 16:59

0 Answers0