So I'm having a problem with including a file on a PHP page called from an AJAX request:
$(document).ready(function() {
$("#image-status").html('Processing images...');
});
function process_images(files) {
$.ajax ({
type: "POST",
url: "includes/imageprocessor.php",
data: {files: JSON.stringify(files)},
success: function(data){
$("#image-status").html(data);
}
});
}
I'm trying this require_once on imageprocessor.php:
require_once("db_connect.php");
Which is also in the include directory, and works: I just used it on another page to run an insert that worked just fine. But when I try to run:
function get_files($id) {
$query = $db->prepare("SELECT * FROM files INNER JOIN file_profile_join ON files.id = file_profile_join.file_id WHERE file_profile_join.profile_id = " . $id . " GROUP BY files.id");
try {
$query->execute();
$rows = $query->fetch();
} catch (PDOException $e) {
die($e->getMessage());
}
}
later on in imageprocessor.php, it errors out:
Fatal error: Call to a member function prepare() on a non-object in /home/yogabopvolume26/theprivatesector.org/epic/includes/imageprocessor.php on line 90
Like it's not requiring db_connect in the first place (that's where $db is declared). But it's also not returning an error that it can't find db_connect.php.
basename(__DIR__)
on imageprocessor.php (echoed through the AJAX request) returns "include."
I've tried using both an absolute URL and root path in the require_once, but then it neither errors out nor continues with imageprocessor.php: no echoes from that file after the require_once are returned. I also can't get the current session from session_start (I can print_r $_SESSION from the originary page [profile.php], so the session is active). Maybe they're related? I can't figure out what's wrong.
Hope your db_connect.php
is something link:
try {
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
And if you are using the connection object outside for your connection file, then you have to access that object into your particular file with the use of global
And as i know, you getting this error because you didn't called the object $db
in your file imageprocessor.php
, try with this:
function get_files($id) {
global $db;
$query = $db->prepare("SELECT * FROM files INNER JOIN file_profile_join ON files.id = file_profile_join.file_id WHERE file_profile_join.profile_id = " . $id . " GROUP BY files.id");
try {
$query->execute();
$rows = $query->fetch();
} catch (PDOException $e) {
die($e->getMessage());
}
}