1

the web shows full blank text when the sentence has a special character (í,ú,ó,é,á)in the database column.

The database its in utf8_general_ci and all the columns.

index.php

    <?php

    require_once 'db.php';

    $query = "SELECT * FROM Frases ORDER BY rand() LIMIT 1";

    $result = mysqli_query($con, $query);

    while ($row = mysqli_fetch_array($result)) {
        print(json_encode($row['frase']));
    };

?>

db.php

<?php

    // Create connection
    $con=mysqli_connect("host","user","pass","database");

    // Check connection
    if (mysqli_connect_error()){
        echo "Faild to connect to MySQL: " . mysqli_connect_error();
    }


?>

I have tried in different rows of the database to take out and place different special characters, and whenever they are present, and I reload the page, these rows appear empty.

Thank you.

When i try mysqli_set_charset($link, 'utf8'); in the index.php before the SELECT dosent do anything.

And $mysqli->set_charset('utf8'); it crashes the page.

UPDATE: index.php

<?php

    require_once 'db.php';
    mysqli_set_charset($con,'utf8');
    $query = "SELECT * FROM Frases ORDER BY rand() LIMIT 1";

    $result = mysqli_query($con, $query);

    while ($row = mysqli_fetch_array($result)) {
        print(json_encode($row['frase']));
    };

?>

db.php had no change

UPDATE 2 Solution: index.php

<?php

    require_once 'db.php';

    mysqli_set_charset($con,'utf8mb4');

    $query = "SELECT * FROM Frases ORDER BY rand() LIMIT 1";

    $result = mysqli_query($con, $query);

    while ($row = mysqli_fetch_array($result)) {
        print($row['frase']);
    };

?>
TheX_H
  • 105
  • 1
  • 1
  • 11
  • Can you define "special characters"? – Funk Forty Niner Oct 23 '19 at 22:51
  • @FunkFortyNiner yes, i refer to (í,ú,ó,é,á) – TheX_H Oct 23 '19 at 23:49
  • Ok. Well, seeing the edit. If it crashes the page, see what logs say and/or enabling and using PHP's error reporting, set to catch and display. – Funk Forty Niner Oct 24 '19 at 00:34
  • Looking at your edit again. You have `$mysqli` and `$link`. Both are wrong. You're using `$con` as the connection. – Funk Forty Niner Oct 24 '19 at 00:36
  • @FunkFortyNiner yep, im sorry with ```$con``` it work, but its show me the unicode character , using ```mysqli_set_charset($con, 'utf8');``` – TheX_H Oct 24 '19 at 00:51
  • Can you update your question with the actual code? That way we'll know or hope to see if there's something you might have missed or didn't use properly. – Funk Forty Niner Oct 24 '19 at 01:21
  • Another thing. You need to have followed everything in the duplicates to the letter that @mario closed it with. By "everything", we/I mean *everything*. For example; which encoding was used to save the files as. There's more info on this in the duplicate(s). – Funk Forty Niner Oct 24 '19 at 01:24
  • @FunkFortyNiner i update the question now, i think i do everything like the duplicates, thats why i still here, i dont know why dosent work – TheX_H Oct 24 '19 at 01:38
  • What happens if you remove the `json_encode()` function? – Funk Forty Niner Oct 24 '19 at 02:20
  • @FunkFortyNiner its work perfectly, Thank you very much, so what its this happend? – TheX_H Oct 24 '19 at 02:37
  • You're welcome. It's hard for me to find out why it did that. Maybe it's because you're trying to encode something that isn't JSON or you need to pass a constant's flag, per the manual https://www.php.net/manual/en/function.json-encode.php - I'd have to read more on it, but maybe the reason lies in there. Maybe @mario could shed some light on this or someone else. I am not familiar with JSON. Cheers – Funk Forty Niner Oct 24 '19 at 12:14
  • @FunkFortyNiner Thank you very much. – TheX_H Oct 24 '19 at 16:40
  • Welcome, anytime :D – Funk Forty Niner Oct 24 '19 at 16:41

0 Answers0