PHP superscript character causing MySQLi select query to find 0 lines

I am using PHP 5.3.3 and MySQL 5.1.61. This column uses UTF-8 encoding, and the PHP file is encoded in UTF-8 without specification.

When you execute a MySQLi query with the ² symbol in SQLyog on Windows, the query is executed correctly and the correct search result is displayed.

If I make the exact exact request in PHP, it will execute but show 0 affected_rows.

Here is what I tried:

  • Using LIKE instead of =
  • PHP file encoding change in ANSI, UTF-8 without specification and UTF-8
  • Executing 'SET NAMES utf-8' and 'latin1' before running the query
  • Was there a header ('Content-Type: text / html; charset = UTF-8'); in php
  • Crash when using MySQLi :: real_escape_string
  • Executing filter_var ($ String, FILTER_SANITIZE_STRING)
  • Tried MySQLi stmt binding

The only way I could get it to work correctly is to exchange files ² for% and change = for LIKE in PHP.

How can I get it right in PHP when using ²?

+5
source share
1 answer

You should be able to make the request work by providing the following:

PHP Fit for UTF-8

, PHP, , UTF-8. UTF-8, . Firefox , , " ". , UTF-8 . UTF-8, . header(), :

header('Content-Type: text/html; charset=UTF-8');

meta head:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

MySQL UTF-8

, , UTF-8. , , . , , , . , , , :

SHOW VARIABLES LIKE 'character_set_system';
SHOW VARIABLES LIKE 'character_set_database';

:

(CREATE | ALTER) DATABASE ... DEFAULT CHARACTER SET utf8;

, , :

SHOW CREATE TABLE myTable;

, , :

(CREATE | ALTER) TABLE ... DEFAULT CHARACTER SET utf8;

, , . , . , ​​ UTF-8, alter :

ALTER TABLE ... CONVERT TO CHARACTER SET utf8;

! UTF-8 , . .

MySQLi UTF-8

, , , , , , UTF-8. :

$db = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

// Change the character set to UTF-8 (have to do it early)
if(! $db->set_charset("utf8"))
{
    printf("Error loading character set utf8: %sn", $db->error);
}

, , . , , 5 HTML: <, >, ', ", and &. htmlspecialchars().

( ), , . : : 1 : 2. !

+3

All Articles