不能将PHP文件包含到我的HTML中

Jer*_*lon 4 html php xampp

我正在尝试访问我的function.php文件,因此我将能够使用其中的函数.但我无法连接它.我尝试单独运行PHP,没有遇到任何问题.但是当我将它包含在我的HTML中时,就会发生这种情况.

HTML代码:

 <!DOCTYPE html>
<?php 
include("functions/function.php");
?>
<html>
    <head>
    <title>Online Shop</title>
    <link rel='stylesheet' href='../CSS/style.css' media="all"/>
    </head>
<body>  
            <!--Main Wrapper starts here!-->
    <div class="main_wrapper"> 

            <!--Header Wrapper starts here!-->
        <div class="header_wrapper" >
        <img id="logo" src="../Pictures/logo.png"/>
        <img id="banner" src="../Pictures/green-banner.jpg"/>
        </div>
            <!--Header ends here!-->

            <!--Menu Bar starts here!-->
        <div class="menubar">
        <ul id="menu">
            <li><a href="#"> Home </a></li> 
            <li><a href="#"> All Products </a></li>
            <li><a href="#"> My Account </a></li>
            <li><a href="#"> Sign Up </a></li>
            <li><a href="#"> Shopping Cart </a></li>
            <li><a href="#"> Contact Us  </a></li>
        </ul>
        <div id="form">
            <form method="get" action="results.php" enctype="multipart/form-data">
                <input type="text" name="user_query"  placeholder="Search Product"/>
                <input type="submit" name="search" value="search"/>
            </form> 
        </div>
        </div><!--Menubar ends here!-->

            <!--Content Wrapper here!-->
        <div class="content_wrapper">
            <div id="sidebar"> 
                <div id="sidebar_title"> Categories</div>
                <ul id="cats">
                    <?php getCats(); ?>             
                </ul>   
            </div>
            <div id="content_area">THis is asa content</div>
        </div><!--Content Wrapper ends here!-->
        <div id="footer">  </div>
    </div><!--Wrapper-->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

PHP代码:

<?php

$con = mysqli_connect("localhost","root","","ecommerce");
//Getting Categories
function getCats(){

global $con;
$get_cats = "Select * from categories";

$run_cat = mysqli_query($con, $get_cats);

while ($row_cats=mysqli_fetch_array($run_cat)){

    $cat_id = $row_cats['cat_id'];
    $cat_title = $row_cats['cat_title'];
echo"<li><a href='#'>$cat_title</a></li>";
}
} //getCats() ENDS HERE
?>
Run Code Online (Sandbox Code Playgroud)

附加信息:

我的HTML路径:C:/xampp/htdocs/ProjectWebDev/HTML/index.html

我的PHP路径:C:/xampp/htdocs/ProjectWebDev/functions/function.php

Fun*_*ner 5

"但是当我将它包含在我的HTML中时,就会发生这种情况."

您需要指示Apache将.html文件视为PHP.

如果你还没有,那就去做吧..html默认情况下,文件不解析PHP指令.

请教:使用.htaccess使所有.html页面以.php文件的形式运行?

.htaccess使用以下内容创建一个调用并放置在服务器根目录中的文件:

AddHandler application/x-httpd-php .html

如果您决定使用.htaccess方法,请记住重新启动服务器/ services/Apache/PHP.这些更改在您执行之前不会生效.

另外,请确保您安装了Web服务器/ PHP并且配置正确.

如果您尝试在Web浏览器中访问文件,例如:

c://file.html或者c://file.php,那将是行不通的.

它必须像这样使用,例如:

  • http://localhost|example.com/file.html

  • http://localhost|example.com/file.php

还要确保包含的路径正确无误.

错误报告添加到文件的顶部,这将有助于查找错误.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code
Run Code Online (Sandbox Code Playgroud)

旁注:显示错误只能在分段中完成,而不能在生产中完成.