I think probably what’s happening is that you’re creating your DB connection outside of the function, and then trying to call it inside of the function without passing it in as an argument.
And I would write sql queries in all other pages which was working.
But in order to make it work for functions I had to remove it from config and I had to put this code above every query I am doing in every page:
$conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
Earlier was fine but this is also okay. I have ot put this $conn above every query in order for it to work now whether in functions or in normal code. Am i doing it correctly ?
You could do global $conn for it to work inside the function. Or create a class that inherits the DB function and then write all your functions as part of that class.
class db_functions {
var $conn = null;
public function __construct($conn) {
$this->conn=$conn;
}
public function simple_test() {
return $this->$conn->prepare("SELECT * FROM categories WHERE catg_pid = '0' AND catg_status = '1' ORDER BY catg_name ASC");
}
}