-1

First off my code:

    <?php 

        include($_SERVER['DOCUMENT_ROOT'].'/website/dbConnection.php');


        function filterTable($searchquery)  
        {   
            $filter_Result = mysqli_query($GLOBALS['connect'], $searchquery);
            return $filter_Result;
        }


        $searchquery = "SELECT Titel, Vorname, Name, Unternehmen, Gruppe FROM mitglieder";
        $searchresult = filterTable($searchquery) or die("Tabelle kann nicht angezeigt werden");

        echo json_encode($searchresult);

    ?>

I found multiple posts, the most popular one being this one.

So I tried this:

    $filter_Result = mysqli_query($GLOBALS['connect'], 'SET CHARACTER SET utf8', $searchquery);

I also put it before the connect variable and in its own function because I wasn't sure how to utilize it but nothing helped. When I put it as a parameter it actually gave out a parse error.

When I echo out the array before sending it out its perfectly normal. After I use json_encode() everything gets wiped and I have an Array full of null's. You can see that in the picture below. I echoed out the Array and then I echoed json_encode with the Array as a parameter. The yellow marked line is the json_encode one, where you can see that suddenly everything is null.

Anybody know what Im doing wrong?

Edit: Forgot to mention that those are German words in my query. Just ignore those, since the problem doesn't lie in the SQL Query.

Edit 2: The proposed post doesn't answer my question as it isn't a similar problem. The other users problem is working with multidimensional arrays and them returning different results. While it is close to what my Problem was, it wasn't the same Problem and it also wasn't the same solution.

Omkar C.
  • 755
  • 8
  • 21
w00ds98
  • 141
  • 8
  • 4
    [mysqli_query](http://php.net/manual/en/mysqli.query.php) returns a [result object](http://php.net/manual/en/class.mysqli-result.php). You need to [fetch](http://php.net/manual/en/mysqli-result.fetch-array.php) data of that first. - but seeing that you have an array (after your edit): is this your full code?? – Jeff Aug 28 '18 at 14:43
  • Did you try run ` json_last_error()` to check errors? – dWinder Aug 28 '18 at 14:43
  • Possible duplicate of [JSON\_ENCODE of multidimensional array giving different results](https://stackoverflow.com/questions/10778741/json-encode-of-multidimensional-array-giving-different-results) – AmmoPT Aug 28 '18 at 14:45
  • *"When I echo out the array before sending it out its perfectly normal"* – What array are you echoing out where? – deceze Aug 28 '18 at 14:52

1 Answers1

3

As @Jeff comment: mysqli_query returns a result object.

See mysqli_fetch_all Documentation

Try run:

$data = mysqli_fetch_all($searchresult);
echo json_encode($data );
AymDev
  • 6,626
  • 4
  • 29
  • 52
dWinder
  • 11,597
  • 3
  • 24
  • 39
  • That is indeed it! Thanks! Also thanks to @Jeff! Also you missed a semicolon there ;) – w00ds98 Aug 28 '18 at 14:52
  • 1
    Since you weren't fetching, where did you get the array that you posted in your question??? – AbraCadaver Aug 28 '18 at 15:02
  • @AbraCadaver its an Array I use in a different part of the site. Basically I use it in the frontend in a table that presents values from my databse. While in this Problem I use it in the Backend and use Ajax because I want my backend to stay on the same site, no matter what table youre editing, unlike in the frontend where you change sites when viewing different tables. – w00ds98 Aug 29 '18 at 08:15