2018-06-17 10:55
浏览 92


This question already has an answer here:

I am trying to make an piece of code that retrieves data from my mysql server and refreshes its content every second without refreshing the page.

When I execute the code below I get an error on the screen:

Warning: require_once(../cfg/db.php): failed to open stream: No such file or directory in \php\classes\update.php on line 9

Fatal error: require_once(): Failed opening required '../cfg/db.php' (include_path='C:\xampp\php\PEAR') in \php\classes\update.php on line 9

The path is set correctly, because when I include the file that is not going via the ajax call it works. So somehow the ajax call does not want to look into another directory. Anyone an solution for this?

In this file The ajax call is being initialized.


function retrieveUpdate(){
        type: 'post',
        url: '../php/classes/update.php',
        data: 'type=test',
        dataType: 'json',
        success: function(index){
            console.log("LOG: "+index);

        timeout: 1000,
        error: function(error){


require_once('../cfg/db.php'); // folder is /php/cfg/db.php

class Update

    public function __construct(){


    public function getUpdate(){
        $db = new DB();

            $query_retrieve_update = $db->db_connection->prepare("SELECT update_id FROM update");

            while($result_retrieve_update = $query_retrieve_update->fetch(PDO::FETCH_ASSOC)){
                $json_retrieve_update[] = $result_retrieve_update;
        print json_encode($json_retrieve_update);


    $update= new Update();

    if($_POST['type'] == 'test'){


define("DB_HOST", "localhost");
define("DB_NAME", "xxx");
define("DB_USER", "xxx");
define("DB_PASS", "xxx");

class DB
    public $db_connection = NULL;

    public function databaseConnection(){
        if($this->db_connection != NULL){
            return true;
                $this->db_connection = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.'', DB_USER, DB_PASS);
                return true;
            }catch(PDOException $e){
                $this->errors[] = DB_CONN_FAIL;
        return false;
$db = new DB();
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • du2229 2018-06-17 11:04

    I think you need to add __DIR__. for some explanation see How to use __dir__?

    打赏 评论

相关推荐 更多相似问题