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>