当包含在另一个文件中时,无法执行php文件,但在作为独立文件打开时执行

当包含在另一个文件中时,无法执行php文件,但在作为独立文件打开时执行,php,Php,我在两个不同的文件夹中有两个名为my_profile.php的php文件:后端和前端 根文件夹有两个称为前端和后端的文件夹。前端文件夹有一个文件my_profile.php。后端文件夹有2个文件my_profile.php和connection.php backend/my_profile.php中的代码是 <?php require_once './connection.php'; $credit_score=mysqli_query($conn, "SELECT

我在两个不同的文件夹中有两个名为my_profile.php的php文件:后端和前端

根文件夹有两个称为前端和后端的文件夹。前端文件夹有一个文件my_profile.php。后端文件夹有2个文件my_profile.php和connection.php

backend/my_profile.php中的代码是

    <?php
    require_once './connection.php';
    $credit_score=mysqli_query($conn, "SELECT credits FROM volunteers WHERE username='$user';");
    $credit=mysqli_fetch_assoc($credit_score);
    $credits=$credit['credits'];
    return $credits;

在frontend/my_profile.php的第4行中,您调用“../backend/my_profile.php”


那么,这个my_profile.php将需要与my_profile.php位于同一目录下的“/connection.php”,即frontend不包含connection.php的目录。

有什么方法可以使用这个文件夹结构而不将connection.php添加到frontend文件夹??这将是很大的帮助…如果是这样的话,“require_once”不会抛出错误吗?既然connection.php不存在,那么“require_once”不会抛出错误吗?@RohanShah您可以使用chdir(“../backend”)将当前工作目录更改为backend;您是否使用
var\u dump()
<?php
error_reporting (0);
$conn = new mysqli("localhost","root","","vector");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php require_once '../backend/my_profile.php'; ?>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Profile</title>
</head>
<body>

    <!-- View credit score -->
    <p>Your credits: <?php echo($credits)?></p>

</body>
</html>