I'm using Bootstrap datepicker and it's supposed to grab information about the date from my database when I change the date.
Right now I'm having my jQuery script in jquery call a PHP function with my database query. My question is, though, should I include my PDO connection string in the function or at the top of the site the datepicker is on?
I just started wondering, if I included it in the function, it would create a new PDO connection object on every date change I suppose. I know that an object last till the object is destroyed, but are there no issues leaving it open through the entire page?
but are there no issues leaving it open through the entire page
Yes, leaving the connection open throughout your request has no issues. But closing and recreating a new connection every time your function is called is an issue so avoid that.
Create a connection only once, use it throughout your code on that page.
Note that since you are using a date picker which is almost always front-end JavaScript code, it will make a new AJAX request to your php page everytime you change a date so it will be a new request everytime and your database connection will be opened/closed with each new request.
If you have your connection on top of the page date picker is on, it can not be shared with a new request just like that using javascript.
Connection in this case has to be on the page which servers the database call on that very request
A little more, to help with your question about closing the connection.
Closing a Connection
Upon successful connection to the database, an instance of the PDO class is returned to your script. The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.
Persistent connections
Many web applications will benefit from making persistent connections to database servers. Persistent connections are not closed at the end of the script, but are cached and re-used when another script requests a connection using the same credentials. The persistent connection cache allows you to avoid the overhead of establishing a new connection every time a script needs to talk to a database, resulting in a faster web application.
Reference
Related
I have this issue:
new MySqli
new Session - with the mysqli as parameter
$_SESSION Usage
close MySqli
But at the end, after I close MySqli, Session::_write is called. Can anyone tell me how can I securely close my MySqli connection after everything is done? Or I don't actually need to? I was thinking that every time the page is accessed a new MySqli connection is opened and never closed, which brings me to thinking I might get some kind of overflow sometime. Any Information would be much appreciated.
The PHP parser is only running when there is a script to process. How does it know when a script needs to be processed? Your web server sends them to it and outputs its data via http to the client. Every time the php process ends (end of script) it disconnects from the sql server, unless you've specified it to leave the connection open.
I would not be worried about it leaving the connection open due to the last Session::_write call, though I would call Session::write_close before closing the SQL connection if you are using it for sessions just to be safe and make sure all data is written back to the db.
To say I am new to PostgreSQL is an understatement. As such I have spent a great deal of time in the last couple of days going over the various manuals at http://www.php.net/manual/en/ref.pgsql.php and at http://www.postgresql.org/docs/9.1/interactive/index.html .
Short form of my question:
Do different users (logged in from separate IP addresses) utilize the same connection to a PostgreSQL data base behind the scenes?
Long form of the question:
In a given php script the database connection $connection is defined near the very beginning of script. That connection is then used throughout the rest of the script via $GLOBALS['connection']. Thus in that script a given user simply reuses the same connection over and over again.
A second user using the same script while logging in from a different location also uses a single copy of the connection.
From the manual (at http://www.php.net/manual/en/function.pg-connect.php):
If a second call is made to pg_connect() with the same connection_string as an existing connection, the existing connection will be returned unless you pass PGSQL_CONNECT_FORCE_NEW as connect_type.
So, does this mean that both users are sharing the same connection (unless the PGSQL_CONNECT_FORCE_NEW flag is sent)?
No, every time you run php script - you make new connection unless you're using persistent connections or connection pooler (like pgbouncer or pgpool).
PGSQL_CONNECT_FORCE_NEW flag means that if inside one php script you call twice pg_connect() with same params you really got one connection unless this flag is set.
I noticed there is no close function for PDO. Should I close the connection or is it unnecessary for PDO?
Upon successful connection to the database, an instance of the PDO class is returned to your script. The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.
http://php.net/manual/en/pdo.connections.php
So the answer is no, you don't need to do anything unless you need to explicitly close the connection during the script execution for whatever reason, in which case just set your PDO object to null.
This question is depending a bit on the type of project and the type of connection.
In almost all of my projects I never manually close the connection. In PHP the connection (unless it is a persistent connection) will only be open during the request. So manually closing it is pretty useless anyway.
When looking at my projects where there was no persistent connection it would have been very hard to know when to manually close the connection either way. Once a project gets larger than a couple of files (and the individual components have no idea about eachother like they should) it becomes very hard to know when the connection will still be needed.
And opening the connection again when needed is waay more expensive than just leaving it open during the request.
Something though when working with persistent connection there will be situations where you will want to manually close the connection.
So to answer your question:
I noticed there is no close function for PDO.
You can nullify the object reference (and all references to the object) to manually close the connection in PHP.
Should I close the connection or is it unnecessary for PDO?
In most situations it is not necessary.
From the PDO's connection page
Upon successful connection to the database, an instance of the PDO class is returned to your script. The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object.
If you don't do this explicitly, PHP will automatically close the connection when your script ends.
EDIT
I'd rather use persistent connection. Though, it's a good practice to close all connections at the end of the script.
I have website which uses database, I have implemented singleton pattern to connect to the database.
But my website uses lots of Iframe or inner pages which in turn connects to database using a singleton pattern in each page.
I have few questions to be clarified
1)if a user A comes to website, he browses 10 pages, does that for all the pages only one connection is created or for every page one connection is created?
2)For all the DB connections amde in the sub pages will use the same connection or a new one ?
3)When the user exists from the website, how to close the DB Connection?
I am finding tricky to close the DB Connection, Suppose in my index.php, if it has menu.php, stats.php and profile.php, I am not able to make out when to close the connection?
What is the best pratice to close the connection?
Website is build on PHP and MYSQL database.
If you use a singleton, then the connection is established once for every request.
For every of the 10 pages.
If you include your sub pages via iframes, then you're opening a new connection for every sub page.
One option would be to have a single entry point for your webpage (a front controller) and then closing the connection, after the request has been handled. If you include your php pages in your index.php file, then close the connection after the include/require statements.
Some developers believe it is good practice to call for a close of connection at the end of their pages. Some of them also spend time freeing variables and arrays at the end of a script. To me this is a waste of bytes. It's ok to do things just because they are good coding practice but in my opinion, we should be aiming for fast loading web pages and the efficient use of databases in the exchange of data (to and from). The fact is, when you close a php script or navigate to another page, unless both pages are using php sessions then the previous page and all that was on it will die. It no longer exists and php dumps it.
There is no way to retrieve any of that information once this happens as it is truly killed.
As for database connections, they close themselves.
The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close(). This is stated in the manual under mysql_connect();
Once again, there are those that like to put a line of code at the end of their scripts calling mysql_close($DB) but remember the waste of bytes thing?
If it is already being done, leave it along. Just my 2 cents worth.
I am using PHP to query the MySQL database on my website. Please answer the following questions:
What will happen if I don't use mysql_close() when I am done with querying the database in the end? The connection will remain open? If yes then upto how much time? If no then why?
If I open and close a connection to MySQL at several places in a
webpage, how is the performance affected? i.e. connection is made again everytime some access to database is required on a single webpage.
How is mysql_close() related to performance? Should I open a new connection everytime some access to database is required OR should I keep only one connection and close it in the end?
If I don't close the connection, then if the user is trying to
access some data again, will the new connection be used or the old
open connection will be used?
It will automatically close when the PHP script is done running during destruct phase.
Performance will negatively be affected. Opening a new socket (especially to an external database server) is more expensive and time consuming than just keeping a pointer to the current connection in memory.
See answer no. 2
The access to the data will be performed by a new PHP request. Hence, you will have a new database connection. No problems there.
I'd advise to open your database connection during construct phase, re-use that connection during the entire execution of your script (if it's OO based, assign a class variable for your database connection and use $this->db during the entire script), and close it during destruction (or don't bother at all closing it, as it will be closed anyway, even when not declared specifically).
From php.net :
Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution.
for performance it depends on situations, how long it is used, for how long it is idle and so on (e.g. long execution). In most cases, there is singleton pattern by which you have one open connection, and make all queries with that open handle. But it's not true all in all, as mysql_connect itself is kind of supports that:
If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters.
So basically, mysql_close is not very needed when it comes to short running scripts.
There is negligible performance loss to close a connection, compared to the resource usage of keeping it open when you don't need it. Better to close a connection as soon as you're done with it, and open it again when you need to.
If you use non-persistent connections all opened MySQL connections will be automatically closed when the PHP script finishes execution.