0

I am in the middle of creating a dashboard which pulls in Zendesk information and stores it in to a DB. The API calls are working as expected, but I am having some issues with the require / include side of things.

Here is my current file structure.

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

Above is the base directoy.

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

Above is the lib directory.

On my index file, I am using AJAX to load pages so the whole page doesn't need to refresh, this woks fine. Now to my question, in the current state (dashboard2/lib/dashboard_functions.php) works, if I print the results it will work

Here is some of the code

<?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'];
    }
}

If I was to call this function on this page it would return (1).

Now to the problem the file dashboard.php has this requirement

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

If I change that include to require, the page will break with this error.

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 

However when it's an include, the actual php code inside the dashboard.php file does not work, the code is

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

It has syntax errors.

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

It returns no values. I have looked online as it's because the file needs to be a "require" rather than a "include" but I can't get the include to work.

Any idea how I can solve this?

Runt
  • 55
  • 5

1 Answers1

0

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

^This helped alot!

I used this syntax

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

And it fixed my problem

Community
  • 1
  • 1
Runt
  • 55
  • 5