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 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度