I have a .php
file called db_conn.php
which establishes a connection to the database. Here is the code of the file.
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "db_name";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: ");
}
else{
}
?>
When I need to do some database operations from some other file for example from a login.php
file, what I do is I use the include_once()
function providing the location to the db_conn.php
as the parameter. I do the same for another PHP file which needs DB access. I have a few doubts regarding this practice/method.
1. Is it okay to do this since I use the include_once()
function and call the DB connection script more than once? Will it open new connections each time? If yes how should this be implemented?
2. If someone tries to include the file from an external domain by using include_once("www.mywebsite.com/php/db_conn.php")
will they be able to execute DB queries on my DB?