As the title says, I have a database connection file in a different directory and im trying to access that inside a function, it works if its not inside a function as I have been using through out my web development, but now that I need to access inside a function it does not work.
function getSubCat($mainCat){
include"../sql/sqln.php";
$sql = "SELECT SUB_CAT FROM CATEGORIES WHERE CAT = '$mainCat' GROUP BY SUB_CAT";
$getQuery = $connection->query($sql);
}
PHP
storms points out this error
. unidentified variable 'connection
'
Database connection
<?php
session_start();
$username = "xxx";
$password = "xxxx";
$database = "xxx";
$localhost = "xxxx";
$connection = mysqli_connect($localhost, $username, $password, $database);
?>
UPDATE:
I tested it and the function is actually working, but PHP storms still give me an warning/error (unidentified variable 'connection'). not sure if I should just ignore it since its actually working.
Send the $connection variable as a parameter:
function getSubCat($mainCat,$connection){
include"../sql/sqln.php";
$sql = "SELECT SUB_CAT FROM CATEGORIES WHERE CAT = '$mainCat' GROUP BY SUB_CAT";
$getQuery = $connection->query($sql);
}
and when you call the function do it this way:
getSubCat($mainCat,$connection)