I have three .php
files.
db_conx.php
$db_conx = mysqli_connect("localhost", "admin", "admin", "gestiune");
// Evaluate the connection
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
}
functions.php
function deplata($pid){
$sqlp = "SELECT * FROM plati WHERE user_id";
$queryp = mysqli_query($db_conx, $sqlp);
$type = "day";
$payments = 0;
$salary = 80;
$days = 4;
$topay = 0;
while($getplata = mysqli_fetch_assoc($queryp)){
$plati += $getplata['valoare'];
}
if($tip == "day"){
$topay = $days * $salary;
}
return $topay;
}
And I have the index.php file to call the files and use them.
include_once("php_includes/db_conx.php");
include_once("php_includes/functii.php");
$salariu = deplata(5);
echo $salariu;
The problem is that it will not connect to the database and it returns some errors:
Notice: Undefined variable: db_conx in D:\xampp\htdocs\manager\pages\php_includes\functions.php on line 5
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in D:\xampp\htdocs\manager\pages\php_includes\functions.php on line 5
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in D:\xampp\htdocs\manager\pages\php_includes\functions.php on line 11
And then it prints the value (since it does not currently use database any values).
What I tried: I tried to put the functions.php
content directly into the index.php
file, I tried to call the db_conx.php
file inside the functions.php
file.