0

I'm trying to get data from a database using PHP, then use JSON to show it in a array. The problem is when I remove the comment-tags on one of the 3 rows, none of the text does'nt longer appear in the browser.. Why?

<?php

    //koble til database
    $mysqli = mysqli_connect('localhost', 'root', '','dump');
    //Lager query til databasen
    $select = mysqli_query($mysqli,'SELECT * FROM authors,articles    WHERE authors.author_id = articles.author_id ORDER BY articles.author_id AND articles.article_id ASC');  
    //Oppretter liste
    $rows=array();
    while($row=mysqli_fetch_array($select))
    {
        $rows[] = array('Forfatter_id' => $row['author_id']);
        $rows[] = array('Fornavn' => $row['first_name']);
        //$rows[] = array('Etternavn' => $row['last_name']);
        $rows[] = array('Artikkel_id' => $row['article_id']);
        //$rows[] = array('Tittel' => $row['title']);
        //$rows[] = array('Innhold' => $row['content']);
        $rows[] = array('Publisert' => $row['publish_date']); 
    }
    //Output
    //echo '<pre>';  
    //print_r($rows);  
    //echo '</pre>'; 
    echo json_encode($rows, JSON_PRETTY_PRINT);  

    error_reporting(E_ALL);
    ini_set('display_errors', '1'); 
?>

Any ideas?

Aromefraise
  • 45
  • 1
  • 1
  • 7
  • Do you have error reporting on? Have you checked the log files for possible error messages? – M. Eriksson Oct 05 '16 at 04:14
  • 1
    ...and don't use the deprecated `mysql_*`-functions. They are deprecated since PHP 5.5 and completely removed in PHP 7. They are also insecure. Use MySQLi or PDO instead. – M. Eriksson Oct 05 '16 at 04:15
  • Can you provide the result of print_r ($rows); before echo json_encode() ? – HZS Oct 05 '16 at 04:19
  • Yes, if i put this: echo '
    ';  
               print_r($rows);  
               echo '
    '; Everything shows. With some text I don't want, but it shows
    – Aromefraise Oct 05 '16 at 04:36
  • I don't get any error – Aromefraise Oct 05 '16 at 04:38
  • Do you have error reporting on? If not, turn it on and check the error log. You could also make sure that display errors are turned on (good while you develop) because if you get a blank page when uncommenting those lines, you're more than likely getting errors. http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – M. Eriksson Oct 05 '16 at 04:52
  • I got this now: ' Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in ... on line 23' – Aromefraise Oct 05 '16 at 05:12
  • Did change all mysql_* to mysqli. Still a blank page. – Aromefraise Oct 05 '16 at 05:23
  • Any new errors? Hard to help you debug without seeing the code. And don't forget to check php.net on how to use the `mysqli_*` functions. – M. Eriksson Oct 05 '16 at 06:09
  • I have updated my code. I do not get any error, Not in the log or browser. So if I have done the mysqli correctly (probably don't) I don't se where the problem is.. – Aromefraise Oct 05 '16 at 11:15
  • I see now when I use print_r($) the commented out rows, have some letters that appear like a ?. Maybe thats the cause? Special letters? – Aromefraise Oct 05 '16 at 12:16

1 Answers1

0

My guess here is that the values in 'last_name', 'title', 'content' contain html tags which mess up your browser rendering.
That's why using <pre></pre> everything shows correctly...
You should htmlspecialchars() your database fields values before outputting to html... :-)

MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • Can you show a humble noob like me where to put it in the code? – Aromefraise Oct 05 '16 at 12:28
  • :-) It depends on how you put it in your page... In your example it seem you're just debugging, printing a json to output... So it's difficult to tell... You could try to change `$rows[] = array('Etternavn' => $row['last_name']);` to `$rows[] = array('Etternavn' => htmlspecialchars($row['last_name']));`... but I don't know if it's what you mean... – MarcoS Oct 05 '16 at 12:33
  • One step closer! Now everything shows, except for that htmlspecialchars($row['last_name'])); puts out an empty "". So it looks like this: "Etternavn": "" – Aromefraise Oct 05 '16 at 12:37
  • What do you see in `last_name` using `
    ` (or `view page source`) ?
    – MarcoS Oct 05 '16 at 12:38
  • It is empty. [2] => Array ( [Etternavn] => ) – Aromefraise Oct 05 '16 at 12:41
  • As I suspected. It's empty in MySQL DB... :-( – MarcoS Oct 05 '16 at 12:42
  • How can it be empty and still have specialletters? When I'm inside the DB in workbench, its not empty at all? So confusing – Aromefraise Oct 05 '16 at 12:45
  • What do you mean by *specialletters*? I mean, you are saying an empty result is coming from you SQL query `SELECT * FROM authors,...` for `$row['last_name']`... So I suppose that that field is empty inside the DB... Otherwise it's a different kind of problem: you should investigate futher... But it's out of the scope of this question... – MarcoS Oct 05 '16 at 12:59