PHP: mysql_real_escape_string is not working in version 7.2

Dear Experts,

I have the following codes used to work in PHP 5.0 and suddenly it stop working after I upgraded to PHP 7.2
 
$query_sql = "SELECT * FROM wp_posts WHERE post_title = %s ORDER BY post_title;";
$query = sprintf($query_sql, mysql_real_escape_string($name));
$postquery = $wpdb->get_results("{$wpdb->prepare($query)}");

echo 'Rows: ' . count($postquery);

The row count return 0.

Please help. Thank you in advance.
Van NAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lenamtlCommented:
Hi,

That was removed from v7
http://php.net/manual/en/function.mysql-real-escape-string.php

You should use PDO_MySQL or MySQLi instead

http://php.net/manual/en/migration70.php
Van NAuthor Commented:
Hi lenamtl,

Would you show me some sample codes?

Thanks
lenamtlCommented:
Hi, I'm using PDO.

You will need to adapt all the MySQL request, PHP code and DB connection.
If you are using PHP Storm there is a way to find the code which is not compatible with v7.

You can check a good tutorial here about PDO
https://phpdelusions.net/pdo

You will learn how it work with the prepared statement https://phpdelusions.net/pdo#prepared
DB connection and everything you will need to know,  there is some learning curve

There are some great tutorial on Udemy
https://www.udemy.com/courses/search/?q=php7&src=sac&kw=php7


Note:
I just realized that you are using WP, so you will need to update Wordpress to the latest version and adapt all your custom code.
https://themeisle.com/blog/upgrade-wordpress-to-php-7/
5 Ways Acronis Skyrockets Your Data Protection

Risks to data security are risks to business continuity. Businesses need to know what these risks look like – and where they can turn for help.
Check our newest E-Book and learn how you can differentiate your data protection business with advanced cloud solutions Acronis delivers

Van NAuthor Commented:
Hi,

I already upgraded the WordPress to the current version 5.0.3.

Thanks.
lenamtlCommented:
Where this MySQL request come from?
Van NAuthor Commented:
WordPress custom template.
lenamtlCommented:
Ok then you will need to adapt all your custom template PHP code / MySQL request code to be compatible
or you can use a new theme https://themeforest.net/category/wordpress

This can take a lot of time depending of the theme so I would use a new one and adapt or hire a dev to fix it

There is a tool that you can use to check PHP compatibility
https://wordpress.org/plugins/php-compatibility-checker/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Van NAuthor Commented:
Got it. Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.