0

I have this json string in a row in mysql database

{"editor":"<p>\u067e\u062f\u0631\u0627\u0645<\/p>"}

And want to find this and replace with <a> tag, but for test, I just select it but look like it can't find it, I used this query:

SELECT * FROM `wp_postmeta` WHERE `meta_value` LIKE '%\u067e\u062f\u0631\u0627\u0645%'

What is the problem?

This is original query:

$keyword_j = "\u067e\u062f\u0631\u0627\u0645";

$sql2 = "UPDATE `wp_postmeta` SET `meta_value` = REPLACE(meta_value, '".$keyword_j."', '<a href=\"\/".$link."\">".$keyword_j."<\/a>') WHERE `meta_value` LIKE '%".$keyword_j."%'";

1 Answer 1

1

You need to put escape sequence to achieve this. In Mysql, there is a second layer of escaping involved. So use this

SELECT * FROM `wp_postmeta` WHERE `meta_value` LIKE '%\\\\u067e\\\\u062f\\\\u0631\\\\u0627\\\\u0645%'

To automatically search and replace slash, use this

<?php
$keyword_j = "\u067e\u062f\u0631\u0627\u0645";
$x = str_replace("\\", "\\\\\\\\", $keyword_j);
echo $x;
?>

Update Query

$str = addslashes(htmlentities("<a href='".$link."'>".$keyword_j."</a>"));

$sql2 = "UPDATE wp_postmeta SET meta_value ='".$str."' WHERE meta_value LIKE '%".$x."%'";
Sign up to request clarification or add additional context in comments.

4 Comments

Right. with this query it found. but one thing, how can I escape variable? $keyword_j ? I can not do this manually, I mean add some slashes, is there any way to do this automatically?
Okay, nice. one last thing, in UPDATE query, should escape everywhere? I used $keyword_j 3 times, should use $x instead of all? or just use in LIKE ? because I used $x for all, it didn't nothing
No. your existing update query is fine. Don't change it. It should not be $x. In your case $keyword_j is fine for update query
But it won't update, nothing changed with UPDATE query. It run without error but nothing changed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.