0

I have following problem:

I have this code simplifyied:

$contents = $pdo->query($sql);
$results = $contents->fetchAll(PDO::FETCH_ASSOC);

foreach($results as $row) {
    $string = htmlspecialchars($row['content']);
    array_push($all_matches, array("data" => $string, "id" => $row["page_id"]));
}

echo json_encode($all_matches);

And in my database I have following content for example:

<h1>MRG Beispiel Seite</h1>

<h2>Ein Beispiel für Einbindung einzelner Felder / Strings aus dem Impressum-Modul:</h2>

And my result is following:

[{data: "", id: "8"}]

My page_id displays but not my content, it stays blank, how do I fix this?

The query:

$sql = "SELECT * FROM `cms_page` WHERE page_id = 8";

When i do echo $row["content"] it actually shows me some content, its a string. The problem is the echo json_encode("data" => $row["content"] its blank

SOLUTION:

I fixed that problem by encoding my string to UTF-8 with:

echo json_encode(utf8_encode($row["content"]));
  • 1
    Just to make things totally clear please show us the query – RiggsFolly Mar 04 '20 at 14:56
  • 1
    Please show an excerpt of your database. Like a screenshot. – Robin Gillitzer Mar 04 '20 at 14:59
  • Do a `SHOW CREATE TABLE cms_page;` and copy/paste the output to your question – RiggsFolly Mar 04 '20 at 14:59
  • var_dump($row); exit; what do you see? – delboy1978uk Mar 04 '20 at 15:02
  • i will do this tomorrow in the office, thanks for help –  Mar 04 '20 at 17:31
  • 1
    What you need to do is figure out where the data is going missing. To do this you can narrow it down with `echo` or `print_r/var_dump` commands. For example: after getting the results do a `var_dump($results);`. Inside your foreach do a `var_dump($row);` also do a `var_dump($row['content']);` and a `var_dump($string);` You will then be able to track down where the error is coming from. Are you sure that your database field is called `content`? – kojow7 Mar 04 '20 at 17:42
  • i am 100% sure that the database is filled with content, when i do that command in phpmyadmin it actually show some result, only on the php side it doesnt –  Mar 05 '20 at 06:04
  • Instead of `utf8_encode`, you should set your database connection encoding to UTF-8 to ensure your database already returns UTF-8. You also shouldn’t HTML-encode the data before putting into JSON, it’s superfluous and is just mangling the data. – deceze Mar 05 '20 at 06:22

0 Answers0