I am having a project using PHP and Postgres. I tried to write a function to get all infomation from one class required from user's input. Below is the version does not work. The commented part is the one that did pretty well but I need to put it into function for reusing target.
function getInfoClass($classID) {
$query = "SELECT * FROM Class
WHERE ID = '{$classID}';";
$result = pg_query($db, $query);
return pg_fetch_assoc($result);
}
$row = getInfoClass($_SESSION["classID"]);
echo $row['id'];
//$query = "SELECT * FROM Class
// WHERE ID = '{$_SESSION["classID"]}';";
//$result = pg_query($db, $query);
//while($row = pg_fetch_assoc($result)) {
// echo $row['id'];
//}
I think you might be running into scope issues here. Make sure to read the php error_log since I'm pretty sure that $db is not declared within the function. If you are using classes and its defined within the class then try $this->db. If not using classes, add it to the parameters as &$db (byReference).