Bret Thomas
Bret Thomas

Reputation: 337

Fatal error: Call to a member function fetch() on a non-object?

I am getting the follwing message returned to me

Fatal error: Call to a member function fetch() on a non-object when running the following. It seems sporadic though, any thoughts?

    $cc_transaction_conn = new PDO('mysql:host='.$server.';dbname='.$db2use, $dbuser, $dbpass);
    $cc_transaction_query = $cc_transaction_conn->query("CALLgenInsCCTransactionLog('".$vmcnum."',
                                                                                     ".$_SESSION[val].",
                                                                                    'Enrollment',
                                                                                    'Processor',
                                                                                    'Merchant',
                                                                                    'Auth',
                                                                                    '".$card_expiration_month."',
                                                                                    '".$card_expiration_year."',
                                                                                    ".$_SESSION[enrollment_fee].",
                                                                                    '".strtoupper($_SESSION[forename])."',
                                                                                    '".strtoupper($_SESSION[surname])."',
                                                                                    '".strtoupper($_SESSION[house_number])."',
                                                                                    '".strtoupper($_SESSION[street])."',
                                                                                    '".strtoupper($_SESSION[flat_number])."',
                                                                                    '".strtoupper($_SESSION[town])."',
                                                                                    '".strtoupper($billing_post_code)."',
                                                                                    '".$ip_address."',
                                                                                    @cc_insert_id)");
        $cc_transaction_result = $cc_transaction_query->fetch();
        $cc_insert_id = $cc_transaction_result['cc_insert_id'];
        $_SESSION[cc_insert_id] = $cc_insert_id;
        $cc_transaction_conn = NULL;

Here is the stored procedure:

    CREATE DEFINER=`reliantuksite`@`%` PROCEDURE `genInsCCTransactionLog`(
                        vmc VARCHAR(16),
                        application_id INT UNSIGNED,
                        reas_description VARCHAR(50),
                        in_processor_name VARCHAR(50),
                        merc_name VARCHAR(50),
                        trans_type_description VARCHAR(50),
                        card_expiration_month VARCHAR(2),
                        card_expiration_year VARCHAR(4),
                        amount DECIMAL(6,2),
                        forename VARCHAR(50),
                        surname VARCHAR(50),
                        house_number VARCHAR(255),
                        street VARCHAR(255),
                        flat_number VARCHAR(75),
                        town VARCHAR(50),
                        postal_code VARCHAR(10),
                        ip_address VARCHAR(32),
                        OUT cc_insert_id INT)
    BEGIN

    DECLARE proc_id TINYINT;
    DECLARE pay_reason_id TINYINT;
    DECLARE merc_account_id INT;
    DECLARE trans_type_id TINYINT;
    SELECT processor_id INTO proc_id FROM cc_processor WHERE processor_name = in_processor_name;
    SELECT payment_reason_id INTO pay_reason_id FROM payment_reason WHERE reason_description = reas_description;
    SELECT merchant_account_id INTO merc_account_id FROM cc_merchant_account WHERE merchant_name = merc_name;
    SELECT transaction_type_id INTO trans_type_id FROM transaction_type WHERE transaction_type_description = trans_type_description;
    INSERT INTO
        cc_transaction_log 
            (transaction_sent_date,
            vmc_number,
            application_id,
            processor_id,
            payment_reason_id,
            merchant_account_id,
            transaction_type_id,
            card_expiration_month,
            card_expiration_year,
            amount,
            billing_forename,
            billing_surname,
            billing_house_number,
            billing_street,
            billing_flat_number,
            billing_town,
            billing_postal_code,
            host_ip)
    VALUES
        (NOW(),
        vmc,
        application_id,
        proc_id,
        pay_reason_id,
        merc_account_id,
        trans_type_id,
        card_expiration_month,
        card_expiration_year,
        amount,
        forename,
        surname,
        house_number,
        street,
        flat_number,
        town,
        postal_code,
        ip_address);
    SELECT LAST_INSERT_ID() INTO cc_insert_id;
    SELECT cc_insert_id;
END;;

Upvotes: 2

Views: 7776

Answers (1)

Mārtiņš Briedis
Mārtiņš Briedis

Reputation: 17752

PDO::query(), as written in the manual, can return a PDOStatment object or FALSE if the query fails. If you try to call a methot on a non-object - FALSE, ofcourse it will fail producing the error message that you are getting.

So, check if the query() result isn't false, then call fetch() on it.

Upvotes: 4

Related Questions