Reputation: 141400
How can you have only one declaration of the database connection variable, $dbconn
?
My login script is based on the following procedure
$_SESSION["logged_in"] = true;
I have the database variable only at the beginning of my index.php:
$dbconn = pg_connect("host=localhost port=5432 dbname=masi user=masi password=123");
I do not the connection by db_close()
anywhere in my codes.
I source my login script to index.php which uses my database.
However, I get the standard warnings of not getting access to db when I do not have the variable declaration also in my scripts. If I add the declaration to each of my scripts, I get an access to db.
Upvotes: 1
Views: 927
Reputation: 460
If i remember your other question you're redirecting to login.php? Which means that no the dbconn variable will not exist because it was initialised on another page.
if you required login.php from index.php it would work
$dbconn = pg_connect("host=localhost port=5432 dbname=masi user=masi password=123");
require_once('login.php');
Upvotes: 0
Reputation: 95203
We need to first understand how PHP is running the show here before we can delve into the wide, wide world of database connections.
Each PHP page request fires off a new PHP script. While you may have sessions turned on, that's an internal PHP construct only. The database sees this as a new connection/process connecting to the server. Therefore, it creates a new process on the database for it, so long as you call your pg_connect
code. Otherwise, Postgres has no idea who you are or what session you want to use. Moreover, once the parent process dies, it generally kills the Postgres session (in this case, the PHP script runs and then dies once it's finished). Sometimes these can leave zombies roaming the streets of Postgres, but by and large, it'll just kill them.
If you want to use SQL code in your PHP script, you must connect to the database in that script before making a call to Postgres.
Upvotes: 2