include / require导致语法错误的问题

时间:2014-07-12 20:37:03

标签: php mysql ajax include apache2

我正在创建一个仪表板,它可以提取Zendesk信息并将其存储到数据库中。 API调用正在按预期工作,但我在require / include方面存在一些问题。

这是我当前的文件结构。

root@v-internet:/var/dashboard2# pwd
/var/dashboard2
root@v-internet:/var/dashboard2# ls
admin  config.php  css  dashboard.php  fonts  images  index.php  js  lib  sql

以上是基础指导。

root@v-internet:/var/dashboard2/lib# pwd
/var/dashboard2/lib
root@v-internet:/var/dashboard2/lib# ls
apicall.php  api-functions.php  dashboard_functions.php

上面是lib目录。

在我的索引文件中,我使用AJAX加载页面,因此整个页面不需要刷新,这很好。现在我的问题是,在当前状态(dashboard2 / lib / dashboard_functions.php)工作,如果我打印结果它将工作

以下是一些代码

<?php
include ('../config.php');

function tickets_submitted() {
    global $con;
    $sql = "SELECT tickets_submitted FROM general_statistics";
    $result = mysqli_query($con, $sql);
    while($row = mysqli_fetch_array($result)) {
        echo $row['tickets_submitted'];
    }
}

如果我在这个页面上调用此函数,它将返回(1)。

现在问题是文件dashboard.php有这个要求

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
include ('lib/dashboard_functions.php');
?>

如果我将include更改为require,页面将突破此错误。

anthony-cp.voip.error.log:[Sat Jul 12 21:00:40 2014] [error] [client 82.46.57.58] PHP Warning:  require(../config.php): failed to open stream: No such file or directory in /var/dashboard2/lib/dashboard_functions.php on line 2
anthony-cp.voip.error.log:[Sat Jul 12 21:00:40 2014] [error] [client 82.46.57.58] PHP   1. {main}() /var/dashboard2/dashboard.php:0
anthony-cp.voip.error.log:[Sat Jul 12 21:00:40 2014] [error] [client 82.46.57.58] PHP   2. require() /var/dashboard2/dashboard.php:5
anthony-cp.voip.error.log:[Sat Jul 12 21:00:40 2014] [error] [client 82.46.57.58] PHP Fatal error:  require(): Failed opening required '../config.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/dashboard2/lib/dashboard_functions.php on line 2
anthony-cp.voip.error.log:[Sat Jul 12 21:00:40 2014] [error] [client 82.46.57.58] PHP   1. {main}() /var/dashboard2/dashboard.php:0
anthony-cp.voip.error.log:[Sat Jul 12 21:00:40 2014] [error] [client 82.46.57.58] PHP   2. require() /var/dashboard2/dashboard.php:5 

但是当它是一个include时,dashboard.php文件中的实际php代码不起作用,代码是

        <div class="col-xs-5 col-sm-3 placeholder">
          <h4>Tickets Submitted</h4>
          <span class="text-muted"><?php tickets_submitted(); ?></span>
        </div>

它有语法错误。

4
/var/log/apache2/anthony-cp.voip.error.log:[Sat Jul 12 21:25:24 2014] [error] [client 82.46.57.58] PHP Warning:  mysqli_query() expects parameter 1 to be mysqli, null given in /var/dashboard2/lib/dashboard_functions.php on line 7
/var/log/apache2/anthony-cp.voip.error.log:[Sat Jul 12 21:25:24 2014] [error] [client 82.46.57.58] PHP   1. {main}() /var/dashboard2/dashboard.php:0
/var/log/apache2/anthony-cp.voip.error.log:[Sat Jul 12 21:25:24 2014] [error] [client 82.46.57.58] PHP   2. tickets_submitted() /var/dashboard2/dashboard.php:17
/var/log/apache2/anthony-cp.voip.error.log:[Sat Jul 12 21:25:24 2014] [error] [client 82.46.57.58] PHP   3. mysqli_query() /var/dashboard2/lib/dashboard_functions.php:7
/var/log/apache2/anthony-cp.voip.error.log:[Sat Jul 12 21:25:24 2014] [error] [client 82.46.57.58] PHP Warning:  mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /var/dashboard2/lib/dashboard_functions.php on line 8

它不返回任何值。我已经在线查看,因为文件需要是“需要”而不是“包含”,但我无法让包含工作。

知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

Are PHP include paths relative to the file or the calling code?

^这帮助了很多!

我使用了这种语法

require_once(dirname(__FILE__).'/../config.php');

它解决了我的问题