-1

I want to convert mysql table data into json but unfortunately it does not return anything.

<?php

$con=mysqli_connect("localhost","truem_apps","censored","truemarlon_apps");

if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql = "SELECT * FROM saechsisch";

if ($result = mysqli_query($con, $sql))
{
    $resultArray = array();
    $tempArray = array();

    while($row = $result->fetch_object())
    {
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    }

    echo json_encode($resultArray);
}

mysqli_close($con);
?>

My page is just blank what am I missing here?

Marlon
  • 197
  • 2
  • 16
  • 1
    Checking your error log, for one. – Sherif Aug 31 '16 at 21:44
  • even if the query returned no rows, you should **STILL** see at least `[]` from the empty array getting encoded. If you get literally nothing, then something is killing your script. Or your query is outright failing, returning boolean false, meaning that the entire fetch/encode section is simply skipped. – Marc B Aug 31 '16 at 21:46
  • side note: `$resultArray[]=$row;` cleaner apporach –  Aug 31 '16 at 21:47
  • @MarcB I've changed `$row = $result->fetch_object()` to `$row = mysql_fetch_assoc($result)` and now I get these brackets `[]`. How can I receive the rows? – Marlon Aug 31 '16 at 22:04
  • 1
    That won't work at all since you are using `mysqli` , not `mysql` – Forbs Aug 31 '16 at 22:09
  • I have updated my answer, hope this will help. – Crunch Much Aug 31 '16 at 22:24

1 Answers1

2

You are trying to access a method in this line: $result->fetch_object();

mysqli_query($con, $sql) doesn't return instance of object. use while ($row = mysqli_fetch_assoc($result)) instead.

To encode as JSON object:

echo json_encode($row);
Crunch Much
  • 1,537
  • 1
  • 11
  • 14
  • Thank you for your answer. It helped me so far that now I get this output: `[false,false]` My table consists of three colums (id, varchar, varchar). What's the problem here? – Marlon Aug 31 '16 at 22:35
  • does you table contains data? or it is empty? – Crunch Much Aug 31 '16 at 22:38
  • It actually contains entries for testing. All of them have whitespaces I don't know if that's important. – Marlon Aug 31 '16 at 22:43
  • Your table should contain data to see result – Crunch Much Aug 31 '16 at 22:47
  • My table contains data. I figured out the problem. Some of my entries have "ä ö ü" and that's why the output is false. Is there a possibility to get that data? – Marlon Aug 31 '16 at 22:55