skip to Main Content

I´m working on a WordPress plugin and for a strange reason, I save correctly strings in the database with the char but to display it, even if I don´t use the wordpress way with $wpdb but like this ( Notice : this piece of code was just to check if it was a deeper matter, it´s not to use it ) :

    $query = "SELECT option_value FROM wp_options WHERE option_name = '" . $this->current_options_name . "' LIMIT 1";
    $conn = new mysqli("host", "username", "password", "dbname");
    $output = $conn->query($query);

    if ($output->num_rows > 0) {
        // output data of each row
        while($row = $output->fetch_assoc()) {
            var_dump($row);
        }
    } else {
        echo "0 results";
    }

That displays ? instead of in string values.

Important notices

  1. I don´t have this problem with the char $.
  2. The char is stored correctly in the database, I can see it in the table value inside PHPMyAdmin.

Someone has got an idea ?

2

Answers


  1. It’s a character encoding issue. Characters are represented by numbers, and there’s many character encodings saying what number is what character.

    There’s three, possibly different, encodings involved in your code. The database’s encoding, PHP’s encoding, and the web page’s encoding. They all should agree on what encoding they’re using. Or they must know what encoding the others are using and convert correctly.

    ? indicates a number which has no corresponding character in the character set your web page is using. $ is the same in many character sets so it’s less sensitive to encoding goofs. € is often different, so it’s sensitive to encoding goofs.

    Any point handing text from one system to another could be mangling the text. Passing text from the web page into PHP, inserting text from PHP into the database, fetching text from the database into PHP, and text from PHP to the web page.

    Simplest thing to do is to make sure they’re all using UTF-8, the most common encoding which handles just about every character.

    This PHP Round Table on Character Encoding and UTF-8 in PHP should walk you through fixing the problem. Also the answers to Setting PHP default encoding to utf-8.

    You can check your database character set by following these answers.

    Finally, check the character encoding of your web page. In most browsers this will be in “Page Info”. For example, Stack Overflow has a Text Encoding of UTF-8.


    Note: Do not use string concatenation to put values into SQL queries. Use bind parameters. It’s less buggy and more secure.

    Login or Signup to reply.
  2. use this , where you add the characters set to the conectio0n.
    It does teh same as your test. But secure tehcharacterset is utf8mb4, because that is my default setting. Could be that you must change that to your needs

    $mysqli= new mysqli("host", "username", "password", "dbname");
    $mysqli->set_charset("utf8mb4");
    $query = "SELECT option_value FROM wp_options WHERE option_name = ? LIMIT 1";
    $stmt = $mysqli->prepare($query );
    $stmt->bind_param("si", $this->current_options_name);
     $stmt->execute();
     $result = $stmt->get_result();
     if($result->num_rows === 0) {
       echo '0 results'; 
      } else {
        while($row = $result->fetch_assoc()) {
         var_dump($row);
        }
      }
    
    $stmt->close();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search