0

I'm working on a WordPress plugin and recently I have planned to migrate from SQL to $wpdb. I'm using a method called $wpdb->prepare instead of mysql_real_escape_string but it does not work. Here is what I tried:

Code:

echo $fetch_row;

Output:

a:5:{s:9:"task_name";s:10:"Backup Now";s:9:"mechanism";s:10:"singleCall"

Code:

echo mysql_real_escape_string( $fetch_row )

Output:

'a:5:{s:9:\"task_name\";s:10:\"Backup Now\";s:9:\"mechanism\"

After mysql_real_escape_string function executed you can see the difference in the string.

I came along this question on stackexchange so I used $wpdb->prepare($fetch_row) but the output did not change, it is just like the input.

Suggest me where I'm doing mistakes.

1
  • $wpdb->prepare is not a direct replacement for mysql_real_escape_string, it has more in common with sprintf/printf, and is a little more involved
    – Tom J Nowell
    Commented Jun 29, 2022 at 13:32

1 Answer 1

1

Check the codex - wpdb::prepare expects the first argument to be a sprintf syntax string, followed by the arguments to be injected (again, just like sprintf):

echo $wpdb->prepare( '%s', $fetch_row );

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.