douji0108 2018-06-17 10:55
浏览 93
已采纳

php中的AJAX调用require_once未执行[重复]

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?

/pages/main.php
In this file The ajax call is being initialized.

$(document).ready(function(){
    retrieveUpdate();
})

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

            setTimeout(index,1000);
        },
        timeout: 1000,
        error: function(error){
            console.log(error);
        }
    })
}

/php/classes/update.php

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

class Update
{

    public function __construct(){

    }

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

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

            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(isset($_POST['type'])){
    if($_POST['type'] == 'test'){
        $update->getUpdate();
    }
}

/php/cfg/db.php

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;
        }else{
            try{
                $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();
</div>
  • 写回答

1条回答 默认 最新

  • du2229 2018-06-17 11:04
    关注

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

    require_once(__DIR__.'/../cfg/db.php');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求用matlab求解上述微分方程的程序代码
  • ¥15 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致