I'm trying to create a database connection, with the connection details on another page.
Before, all these details where on the same page as all the other code, like this:
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="database"; // Database name
$tbl_name="table"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
and so on. This is the way I establish my db connection right now. Every page has this code, only the sql is different for each page.
Now, what I'm trying to do:
every page should have:
include_once '../includes/db_connect.php';
the page db_connect.php contains this line:
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
Every page should connect automatically to the database using this code. I don't want to have all the details on every single page. So on the page itself, the $sql should be connected already so I can start my code from the $sql part.
How can this be done? Because now I'm getting an error that the $sql cannot be executed because there's no database connection.