Question

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.

Was it helpful?

Solution

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());
    }
}

OTHER TIPS

Call to a member function someFunc() on a non-object in......

This type of error happens when you want to use the someFunc() method of any object, but unfortunately your object is not a object.

Suppose

$obj = New SomeObj();
$nextObj = $obj->someMethod(); // << Suppoes someMethod() returns a object if successfull
$nextObj->newMethod();

In this code, the $nextObj->newMethod() will work only when $nextObj is an object. But we cannot be sure about that because $obj->someMethod(); will return an object only if successful, and returns otherwise when not successful.

A good way to prevent that would be.

$nextObj = $obj->someMethod(); // << Suppoes someMethod() returns a object if successfull
if(is_object($nextObj)){
    $nextObj->newMethod();
} else {
    //Now you know that there is error while executing $obj->someMethod();
}

Another Thing: Since you have a function function get_files($id). You can yourself see that $db variable is not defined within the scope of your function. So, $db contains nothing for now. AS in the other answers, you should use global $db or can pass the $db object as an argument to your function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top