6

The "×" symbol (not a little x), I believe this is the multiplication symbol, is breaking MySQL records.

The problem is that whenever I try to retrieve a record that has this symbol "×" the record returns as blank.

I am using PHP and WAMP server by the way.

The collation is latin1_swedish_ci. However, changing collations didn't seem to fix the problem. The mysql version is 5.6.17 Here is my function that gets the records from the table and saves it to an object:

public function assign() {
    $SQL = "SELECT * FROM " . $this->tb . " ORDER BY " . $this->order;
    $this->tb_handle = mysqli_query($this->db_handle,$SQL);

    $this->rowNumber = mysqli_num_rows($this->tb_handle);

    //this creates arrays from records given even if the table is empty
    //this is to prevent errors
    if ($this->rowNumber === 0) {
        foreach ($this->records as $record) {
            $this->{$record} = array();
        } 
    } else {
        $i = 0;        
        while ( $db_field = mysqli_fetch_assoc($this->tb_handle) ) {
            foreach ($this->records as $record) {
                $this->{$record}[$i] = $db_field[$record];
                $this->{$record}[$i] = htmlspecialchars($this->{$record}[$i]);
            }            
            $i++;
        }
    }
}

This works for anything that does not have the "×" symbol. i don't know why that symbol should cause the entire record to return as blank.

Zack
  • 874
  • 1
  • 9
  • 18
  • 2
    `SET NAMES 'utf8';`? encoding issues? What is the collation of your table? How are you connecting to the db? – Elias Van Ootegem Aug 22 '14 at 07:22
  • 1
    +4 in less than 5 min strange. – Satish Sharma Aug 22 '14 at 07:26
  • @Elias Van Ootegem Thanks, the answer by Stony solved the record problem of it not showing up at all, but now it shows up as �. The collation is latin1_swedish_ci, any ideas on what to change it to to allow the character to show? Thanks! – Zack Aug 22 '14 at 07:35
  • @user3290786: Sure [`mb_convert_encoding`](http://www.php.net/mb_convert_encoding) is a function that can encode ISO-8859-1 to UTF-8 back and forth [cf this answer of mine](http://stackoverflow.com/questions/25184917/how-to-pass-a-latin1-charset-associative-array-from-php-to-javascript/25185022#25185022) – Elias Van Ootegem Aug 22 '14 at 07:41
  • mb_convert encoding worked I just added `$this->{$record}[$i] = mb_convert_encoding($this->{$record}[$i],"UTF-8");` Thanks! – Zack Aug 22 '14 at 07:55

1 Answers1

3

I see you use the htmlspecialchars until PHP 5.4 the internal encoding is UTF-8. So if you have a record with iso data and you put it in htmlspecialchars you get no result.

In this case you have to set the encoding to iso-8859-1 for example.

To solve the problem you can define an encoding

htmlspecialchars($value, ENT_QUOTES, "ISO-8859-1");

This will fix symbols showing incorrectly:

mb_convert_encoding($value, "UTF-8");

I think this could be your problem.

Zack
  • 874
  • 1
  • 9
  • 18
René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • This mostly worked. The data now displays, it just shows a � where the × should be. Still much better than it not showing up at all. Do you know if I need to set a specific collation to allow that character to show up? Thanks! (oh and one too many ")" on the line of code by the way) – Zack Aug 22 '14 at 07:29
  • @user3290786 Have you tried using htmlspecialchars_decode() when you are printing the string from DB? – kubilay Aug 22 '14 at 07:38
  • Ya, it doesn't appear to change anything though – Zack Aug 22 '14 at 07:42
  • This is an annoying coding problem. Perhaps you can try to encode it to UTF-8. Perhaps then you get the correct return. – René Höhle Aug 22 '14 at 07:43
  • this worked `$this->{$record}[$i] = mb_convert_encoding($this->{$record}[$i],"UTF-8");` combiend with the htmlspecialchars answer you gave, I'll suggest a quick edit to your answer and select it. Thanks! – Zack Aug 22 '14 at 07:58