php – Database Connection not Working as Include File –
I want to move the my database connection info to an include file – normally, the database connects fine when I have it written out in the entire php header like this:
connection.php
create this file.
<?php
$connection_OIC = mysqli_connect("localhost","user","password","dbname");
?>
index.php
file create now
<?php
require_once dirname(__FILE__) . '/connection.php';
if($connection_OIC==true){
echo "OK";
}else{
echo "failed";
}
?>
include require not working
If you are not able to read the connection file by any method, then you follow the steps given below.
In cPanel open the mysqli connection file in which your mysqli_connect(“localhost”,”user”,”password”,”dbname”); Everything was set as you can see in the image
<?php
include('/home4/domainshort/public_html/domain.com/sub.domain.com/folder/connection.php');
if($connection_OIC==true){
echo "OK";
}else{
echo "failed";
}
?>
By copying the path of the URL next to Editing, you include('')
; If you enter and check, then your work will be done.
OR
<?php
include $_SERVER['DOCUMENT_ROOT'] .'/folder/connection.php';
if($connection_OIC==true){
echo "OK";
}else{
echo "failed";
}
?>
Here you have to give the path where your Connection file is, for example my domain name is codinghelp.in, my connection file is a connection.php file in the DB Name Folder, it also has other files which require mysqli, db in it. I can include /connection.php, now I have to open the file in which I want to include the connection file and write include $_SERVER[‘DOCUMENT_ROOT’] .’/db/connection.php’; That’s all you have to write, your connection file will be fetched