PHP函数连接数据库调用查询失败
PHP function fails to connect to the database to call query
我有三个 .php
个文件。
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;
}
我有 index.php 文件来调用这些文件并使用它们。
include_once("php_includes/db_conx.php");
include_once("php_includes/functii.php");
$salariu = deplata(5);
echo $salariu;
问题是它无法连接到数据库并且 returns 一些错误:
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
然后打印值(因为它当前不使用数据库的任何值)。
我尝试了什么:我试图将 functions.php
内容直接放入 index.php
文件中,我试图在 functions.php
文件中调用 db_conx.php
文件。
您需要在 function.php
中定义值。查看下面的代码
function.php
function deplata($pid){
global $db_conx;
$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;
}
index.php
检查index.php
中的错别字
include_once("php_includes/function.php");
我有三个 .php
个文件。
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;
}
我有 index.php 文件来调用这些文件并使用它们。
include_once("php_includes/db_conx.php");
include_once("php_includes/functii.php");
$salariu = deplata(5);
echo $salariu;
问题是它无法连接到数据库并且 returns 一些错误:
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
然后打印值(因为它当前不使用数据库的任何值)。
我尝试了什么:我试图将 functions.php
内容直接放入 index.php
文件中,我试图在 functions.php
文件中调用 db_conx.php
文件。
您需要在 function.php
中定义值。查看下面的代码
function.php
function deplata($pid){
global $db_conx;
$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;
}
index.php
检查index.php
include_once("php_includes/function.php");