The reason the old mysql statements worked inside a function, was because php made the last mysql connection available globally. Only the mysql extension did this, so no other extension you use will work without doing more to get the connection into the function. My reply mentioned the two methods that can be used to get the connection into a function. The first method requires making changes to all of the function calls (and if those calls are inside of other functions, even more changes) and the second method has an issue if there is or will ever be more than one connection.
The reasons for converting to use the php PDO extension are -
1) It is simpler and more consistent than the msyqli extension, especially when using prepared queries. You should use prepared queries when supplying data to an sql query statement because it both simplifies the code (all code attempting to escape data, like the subject in this thread can be removed) and it simplifies the sql query syntax, while preventing sql injection. Sql injection is still possible when using the ...._escape_string() functions if the character set that php is using is not the same as the character set you are using in your database and it is rare that older code even sets up the character set.
2) Once you learn the php statements needed for the PDO extension, with any database type, you can use those same statements with other database types. You will need to make any database type changes to the connection Data Source Name (DSN) and the sql queries, but the php statements will remain the same.
To convert code to use the PDO extension, you will need to learn enough about the php PDO statements to be able to identify which ones to use and how to use them. The php.net documentation contains all the basic information you need to do this, along with examples.
PDO uses OOP (Object Oriented Programming) classes. There are two PDO classes to learn, the PDO class and the PDOStatement class.
For most simple web applications, you should start by looking at the documentation for the following -
PDO class - __construct(), lastInsertId(), prepare(), query(), and setAttribute()
PDOStatement class - execute(), fetch(), fetchAll(), and rowCount()
When you make the connection you need to set the character set, set the error mode to exceptions (using exceptions for errors and let php catch the exception, will further simplify the code your have to write or covert), set emulated prepared queries to false, and set the default fetch mode to assoc. See the following typical connection code -
$DB_HOST = 'localhost'; db host name/host ip - change to match your's
$DB_USER = 'your_database_username';
$DB_PASS = 'your_database_password';
$DB_NAME = 'your_database_name';
$DB_ENCODING = 'utf8'; // db character encoding - change to match your's
$pdo = new pdo("mysql:host=$DB_HOST;dbname=$DB_NAME;charset=$DB_ENCODING",$DB_USER,$DB_PASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // set the error mode to exceptions
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); // run real prepared queries
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); // set default fetch mode to assoc
If you post an example of the query related code you are trying to convert, someone will show how it would look using the PDO extension and if it has data being supplied to the query, what a prepared query looks like.