1

I'm trying to execute a SQL query from within a PHP function but I keep getting the following error:

Fatal error: Call to a member function prepare() on a non-object in /homepages/23/d363590707/htdocs/bNames.php5 on line 14

Line 14 is the line with the prepare method in the following method:

function testing()
{
    $query = "SELECT `no` FROM `brandNames` WHERE `id` = $index";
    $stmt = $dbc->prepare($query);   <--- line 14 -----------------<<<
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($no);
    $stmt->fetch(); 
}

note: when I stick the code block in the page(without using a function) the query works and I don't have any problems.

also, I intend to add parameters to this function to replace the table name, column names, and values. This was just a version that had the least amount of things that could go wrong yet still illustrate my problem.

thanks in advance

Edit: This is what the file looks like:

<?php
    require_once('connectvars.php'); //contains the info used in mysqli_connect

    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);


    function testing($dbc)
    {
        $query = "SELECT `no` FROM `brandNames` WHERE `id` = $index";
        $stmt = $dbc->prepare($query);
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($no);
        $stmt->fetch(); 
    }

//more code
?>
2
  • 5
    Where is $dbc defined? It seems like it's out of scope. Commented May 9, 2012 at 17:14
  • @GioBorje believe it is in global scope, I edited my OP to show what it looks like Commented May 9, 2012 at 17:25

3 Answers 3

4

Although you could define $dbc as global, I would suggest simply passing $dbc into the function:

function testing($dbc)
{
    $query = "SELECT `no` FROM `brandNames` WHERE `id` = $index";
    $stmt = $dbc->prepare($query);   <--- line 14 -----------------<<<
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($no);
    $stmt->fetch(); 
}

Now, when you call testing(), you need to pass in $dbc: testing($dbc);.

Sign up to request clarification or add additional context in comments.

3 Comments

now I am getting an error on the next line (execute). Any ideas why that is happening?
@LoganBesecker $index isn't defined, so your query is invalid: SELECT ... WHERE id = .
oh jeeze, that's a slight oversight haha. Thank you. that solved my last problem. Thank you very much
4

The problem is that the $dbc object (PDO, probably?) is not in the scope of your function. You can read more about this here: http://php.net/manual/en/language.variables.scope.php

To fix this, you could try adding the following line to the top of your function:

global $dbc;

Comments

1

You probably need a global $dbc; at the start of your function to bring it into the function's scope.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.