0

I am unable to understand why this error is popping up. I do know that the obvious reason why these kind of errors come up but I did look and re-look into my code and cannot understand why!

The error is:

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

Here are the code snippets.

db_connect.php

<?php
include_once 'psl-config.php';   // As functions.php is not included
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);

customer.php

include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
.
.
.
function myCallBackFunction($array) {
                //echo ".";
               // print_r($array);

                $amount=$array['amount'];
                $date=$array['date'];                    
                if(transaction($mysqli, $date, $amount))
                {
                    echo "Transaction table Updated";
                }
            }

functions.php

//Function to insert transactions
function transaction($mysqli, $date, $amount) {
    if($smt = $mysqli->prepare("INSERT INTO `tbltransaction` (entry_date,income) VALUES (?,?)
  ON DUPLICATE KEY UPDATE income=income+?;"));
    {
        $stmt->bind_param('sii', $date, $amount, $amount);  // Bind "$date" and "$amount" to parameter.               
        $stmt->execute();    // Execute the prepared query.
        $stmt->store_result();
        if ($stmt->num_rows == 1) {
            return true;
        } else {
            return false;
        }
    }

}

tbltransaction

Column  Type    Null    Default     Comments    MIME
entry_date  date    No           
income  int(11) No 

P.S: another function in the functions.php file is working just fine, here is the function and the way I am calling it

function

//Function to display notice
function notice($mysqli) {
    $notice = null;
    if ($stmt = $mysqli->prepare("SELECT notice FROM tblnotice ORDER BY ID DESC LIMIT 1")) {
        $stmt->execute();    // Execute the prepared query.
        $stmt->store_result();
        // get variables from result.
        $stmt->bind_result($notice);
        $stmt->fetch();
        return $notice;
    }
}

calling the function

<?php echo notice($mysqli); ?>
Sam Richards
  • 15
  • 1
  • 3
Rick Roy
  • 1,368
  • 1
  • 17
  • 41
  • Anything in `customer.php` that could be stomping on the variable? – Dan Smith Mar 13 '15 at 11:21
  • error line number or which line caused this error?? – habib ul haq Mar 13 '15 at 11:22
  • @Bulk now that you mention it, I think that must be the issue let me update the `customer.php` file you will understand what I mean to say – Rick Roy Mar 13 '15 at 11:23
  • @habibulhaq the error is on ` in E:\xampp\htdocs\crm\includes\functions.php on line 25` i.e the `if($smt = $mysqli->prepare("INSERT INTO `tbltransaction` (entry_date,income) VALUES (?,?)` of the `function.php` file – Rick Roy Mar 13 '15 at 11:24
  • @Bulk do you think since I am trying to call the `$mysqli` from inside another function it might not be working? Should I try and make it global? – Rick Roy Mar 13 '15 at 11:26
  • 1
    See my answer, but don't fall for the easy trap of making it global - it's very bad practice :) – Dan Smith Mar 13 '15 at 11:28

2 Answers2

2

This is a variable scope issue - you need to pass the $mysqli object in to the myCallBackFunction function as a second parameter or it won't be set inside that function.

Like this:

function myCallBackFunction($array, $mysqli) {

Then where you call that function you'll need to pass in the $mysqli object:

myCallBackFunction($array, $mysqli);
Dan Smith
  • 5,425
  • 29
  • 32
0

you do alternatively

function notice() {
  global $mysqli;
    $notice = null;
    if ($stmt = $mysqli->prepare("SELECT notice FROM tblnotice ORDER BY ID DESC LIMIT 1")) {
        $stmt->execute();    // Execute the prepared query.
        $stmt->store_result();
        // get variables from result.
        $stmt->bind_result($notice);
        $stmt->fetch();
        return $notice;
    }
}
user1844933
  • 3,179
  • 2
  • 22
  • 37
  • 1
    While this might work it's very bad practice :) see this: http://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice-if-so-why – Dan Smith Mar 13 '15 at 11:29