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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?