1

JSON-String is empty but why? The same code works fine if I change the table. There are 20 entries and the Code works today afternoon but for some reason I can't get the entries anymore :(

    <?php

    // open connection to mysql db
    include("connect.php");

    // fetch table rows from mysql db
    $sql = "SELECT * FROM Videoquestions";
    $result = mysqli_query($conn, $sql);

    // create an array
    $var = array();
    while($obj = mysqli_fetch_object($result)) {
    $var[] = $obj;
    }

    // convert array to JSON
    echo '{"Videofrage":'.json_encode($var, JSON_UNESCAPED_SLASHES).'}';

    // Close the database connection
    $conn->close(); 

    ?>

Need to encode to utf8 ... anybody knows how to? German Umlaute like äöü are blocked and with this line of code it will be solved:

mysqli_set_charset($conn, 'utf8');

j08691
  • 204,283
  • 31
  • 260
  • 272
  • Add error checking, such as `or die(mysqli_error())` to your queries. Or you can find the issues in your current error logs. – Jay Blanchard Aug 27 '15 at 21:37
  • i didn't changed anything ... only the database connection ... I paste it to connect.php. The connection works fine in other scripts – AndroidBeginner Aug 27 '15 at 21:37
  • have you look what is in $var ? and read this : http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php – mmm Aug 27 '15 at 21:38
  • output of $var is only Array – AndroidBeginner Aug 27 '15 at 21:42
  • [27-Aug-2015 23:38:17 Europe/Berlin] PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 130968 bytes) in /Applications/MAMP/htdocs/AndroidVideoUpload/connect.php on line 4 – AndroidBeginner Aug 27 '15 at 21:44
  • I think the issue might be the `JSON_UNESCAPED_SLASHES` constant - this threw errors for me on my dev pc ~ have you checked your error log? It might be the version of PHP that I have but the only constants available to me for `json_encode` appear to be `JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_FORCE_OBJECT` – Professor Abronsius Aug 27 '15 at 21:49
  • yes there isn't an error and if i change it to `echo json_encode($var);` there is no change – AndroidBeginner Aug 27 '15 at 21:52
  • 2
    **The error is occuring in `connect.php`** so add that script to your question. Edit the question, dont paste it onto a comment. – RiggsFolly Aug 27 '15 at 22:06
  • 1
    It does not matter, because I probably dont believe you for 2 reasons **1)** _Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 130968 bytes) in /Applications/MAMP/htdocs/AndroidVideoUpload/connect.php on line 4_ Tells us the error is in connect.php **2)** _i didn't changed anything ... only the database connection ... I paste it to connect.php._ So you did change **connect.php** – RiggsFolly Aug 27 '15 at 22:19
  • I am guessing you have more than 1 _connect.php_ this script will be loading it from the same directory as this script lives in! What is this script called. Unless you are getting it from somewhere on the search path – RiggsFolly Aug 27 '15 at 22:23
  • As I said ... the same code works with other TABLE! – AndroidBeginner Aug 27 '15 at 22:26
  • i paste the code from connect.php to the getjson_vf.php and there is still no change – AndroidBeginner Aug 27 '15 at 22:27
  • Well its getting a different one from somewhere! LINE 4 of this one is not doing anything to do with arrays, and the error message expressly says LINE 4 of CONNECT.PHP Blew the memory limits. I assume you are still getting that error message??? – RiggsFolly Aug 27 '15 at 22:29
  • All the other errors in the php error log also point to the fact that a connection to the database has not been made before trying to access the database. Are you sure you dont have 2 version of this site and you are actually runnning a different version to the one you think!! – RiggsFolly Aug 27 '15 at 22:34
  • yes I'm sure, because I run it through the local server ... I can't find the issue ... I think it's in the database – AndroidBeginner Aug 27 '15 at 22:42
  • Last resort, delete connect.php and recode it from scratch. Just possibly there is some file corruption. I know it sounds silly. But it happens. LIke I said last resort, nothing more I can do from here. – RiggsFolly Aug 27 '15 at 23:01
  • but the problem is if i paste the connection into getjson_vf.php nothing happens :-/ ... big thanks to you ... I'll test your resort :) – AndroidBeginner Aug 27 '15 at 23:04
  • maybe the code works only with UTF-8 encoded data – AndroidBeginner Aug 27 '15 at 23:24

0 Answers0