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
?>
$dbcdefined? It seems like it's out of scope.