I am using pdo in php and the error i am getting is:
Product sub variant: Product: (
Fatal error: Call to a member function prepare() on a non-object
i have this piece of code in a class and i call it in php, the class is included in the page im calling it.
function get_variantname($variant_id)
{
$query = $db->prepare("select parent_attribute_label from tbl_parent_attribute where parent_attribute_id = :variant_id");
$query->bindParam(":variant_id", $variant_id);
$query->execute();
while($row = $query->fetch(PDO::FETCH_OBJ))
{
return $row->parent_attribute_label;
}
}
You are trying to incorrectly access the
$dbvariable, which looks like aglobal. Addat the beginning of your function.
The error message tells you that
prepareis being called on something that is not an object. But we all know it is an object, since you set it once to some value (there’s no chance that it might be e.g. a string). That leaves only the possibility of it not having the value you set inside the function, for some reason. Which leads us to the answer.