PHP5 to PHP7 DatabaseConnection

I am look at an old PHP project that uses mysql_connect, and I am trying to update it to PHP 7, by using mysqli_connect.

In the original project, I had a file - connect.php which had the database info, I would then include that file where needed.  But I cant seem to get this to work in PHP7

Original file looked like this.

// Connects to my Database
mysql_connect("", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());

I have changed to this


$con=mysqli_connect("", "username", "password","Database_Name");


now on  a page where I had

return mysql_real_escape_string($data);

and am now trying to use

return mysqli_real_escape_string($con, $data);

and I get the error con is not defined.

How can i keep the original structure but update to PHP7
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Chris StanyonWebDevCommented:
Add some error checking to your connection page. Turn on error reporting at the start of it, and then explicitly check the connection:

ini_set('display_errors', 1);

$con = mysqli_connect("", "username", "password", "Database_Name");

if (! $con) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;

Open in new window

In the page where you need to use it, try a require_once("connection.php") instead of an include and see what you get. If it fails, you should at least get some useful debugging info.

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
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

From novice to tech pro — start learning today.