Please help to understand, on some cells the request to the database does not go through, called this way:

$stmt = mysqli_prepare($connection, "UPDATE `table` SET `data` = ? WHERE `table`.`data` = ?"); mysqli_stmt_bind_param($stmt, 'ss', $newcontent, $originalcontent); mysqli_stmt_execute($stmt); 

For example, if the contents of the data cell:

 <img src="http://dd.com/1.jpg"> 
  • then everything goes fine.

What if:

 г<img src="http://dd.com/1.jpg"> 
  • then the value in the cell is not updated. If in a cell instead of a Russian letter is a Latin or a digit, then everything goes fine.

From this we can conclude that the most likely problem is in the encoding.

When connecting to the database I use the query:

 mysqli_query($dbconnection,"SET CHARACTER SET 'utf8'"); 

I thought that he would solve all the encoding problems (until that moment he solved, all characters are displayed correctly both when outputting and when entering data into the database - but for the WHERE condition, this is probably not enough).

Tell me, please, how php can solve this problem.

  • r <img src = " dd.com/1.jpg "> as you see this г ? In the browser in the input field you score or on the server you find out in some way (in the logs for example)? Maybe the wrong encoding comes from the browser - Sergey
  • @Sergey I see it through echo $originalcontent; . The browser almost does not participate at all with me, only initiates the launch of the script. The script processes data from the database - where Russian letters are, the condition is not met. - federk 2:58 pm
  • Those. г formed programmatically? Somewhere in the text of the program is written the type of such $str = "г" + чего-то там ? The encoding of the text of the program what? windows 1251, koi8, utf-8? - Sergey
  • @Sergey is not, г already contained in the database that the program is processing. The coding of the program text itself is utf-8 - federk
  • one
    mysqli_set_charset() This is the preferred way to specify a character set. The use of mysqli_query() functions (for example, SET NAMES utf8 ) for this purpose is not recommended. Maybe your case is exactly - Sergey

2 answers 2

User Sergei suggested solution in the comments. If the encoding is declared like this:

 mysqli_set_charset($dbconnection,'utf8'); 

That all goes well.

    1. Try using "SET NAMES utf8" instead of "SET CHARACTER SET 'utf8'".

    2. Try to form a query completely, instead of "UPDATE table SET data =? WHERE table . data =?" write directly "UPDATE table SET data = '$ newcontent' WHERE data = '$ originalcontent'".

    • 1. I tried instead of and together. Does not help. 2. So, too, have tried, to no avail. - federk
    • And what is the base encoding? What is the encoding of the file that the request sends? - Stanislav
    • The file has utf8. The table has a latin1_swedish_ci encoding, but the data column has a utf8_bin . Now I tried to change and the table and columns on utf8mb4_unicode_ci did not help. - federk
    • Specifying the encoding via the mysqli_set_charset() function solved the problem. - federk