When I load a php page on my browser, it connects to the DB and runs some sql... let's say I now follow a link and it takes me to another page within the same website. What happened server wise? Did my first connection to the DB close, and re-opened again? Is that what happens in most cases?
Its very likely that the connection to your database is closed once the page has been processed by PHP, obviously then the result of the PHP is sent to the browser and viewed by the user.
Assuming you're running MySQL the only reason this wouldn't be the case is if the PHP script uses mysql_pconnect, where the connection will be kept open. However its usually good practice not use use this unless the MySQL server and the PHP server have a low bandwidth connection that's unused by other processes.
Yes, in most cases your database connection will close and re-open. In particular if the PHP interpreter is restarted for each page then it has no choice but to do this.
I believe the typical exception (although I've never used this myself) is where you're using something like mod_php.so (for Apache) and you arrange for a DB connection object to be stored as part of the user's session state. I don't believe that's recommended practise, though.
See http://php.net/manual/en/features.persistent-connections.php for more.
That's the usual case yes. But if you're talking about MySQL, you can use mysql_pconnect to keep persistent connections.
It depends on how the PHP was developed. If it was coded to close after each transaction, then yes it will be re-opened each time you view a page.
There is also the concept of a database connection pool. When a connection is used it is not closed but placed into a "pool" of connections waiting to be used again. Once a specified amount of time is passed without the connection being used it is then closed to save resources.
Pooling connections saves on processing time having to reopen a connection on each page reload.
Related
I created a temporary table in one php file, and want to access it in another php file. the scripts run sequentially. I used mysqli and am prepending p: to hostname.
The problem is in my second php file, I cant access my temporary table. So I wanted to know if its possible to do this, or not? And if yes how? Am using WAMP server.
Not possible, directly. Temporary tables are destroyed when the connection used to establish them is closed. When your "create" script shuts down, its DB connection is closed, and mysql cleans - including destroying that temp table.
That means when your "use" script fires up, it gets a new connection, without any of the stuff that the first script did.
There are persistent connections available in PHP, but those connections exist in a pool, and there is no control over WHICH connection any particular scripts gets from that pool. You may get lucky and receive the same connection for two different scripts, but it's purely by chance.
You'd need some OTHER 3rd script that operates continously to hold open the mysql connection, leaving the temp table in place. And your other two scripts would communicate with this third one.
From http://php.net/manual/en/mysqli.persistconns.php
The persistent connection of the mysqli extension however provides built-in cleanup handling code. The cleanup carried out by mysqli includes:
(worth reading the other things too but the important bit is)
Close and drop temporary tables
In short, a temporary table is just that, temporary. It's not meant to be used for other purposes than to temporarily store some data for one specific operation. If you want a more permanent thing consider using a concrete table with a memory storage engine.
I'm wondering, if mysqli_connect() does automatically close at the end of the script. I know, that mysql_connect() does, but I'm speaking about improved mysqli_connect().. Also I found answered question, that says it does, but in official documentation it is not written.. So, can I rely on those not official statements? (Or better to ask, it is safe to rely on them?)
Here's what you should consider. The MySql connection does automatically close when the script terminates. However, if your script does a lot of processing after the data has been retrieved and stored to an array or whatever you store it to, it is safer to close the connection explicitly so you do not face the chance of running out of available connections while the script is busy running the post data retrieval routines. For me, I just explicitly close the connection. It's a habit I've developed and my fingers auto-type the command.
I see many tutorial about connection db is open a connection, and do the stuff, and close the connection in the end. I would like to know whether this is true for real life example?
I know it can get the job done when I open, and close the connection every time. But will it have some performance issue? Moreover, can I reuse the opened connection? Or is this inevitable to keep open and closing the db?? Thanks.
*I am using php + mysql, thanks.
you can use mysql_pconnect if you want a persistent connection:
http://php.net/manual/en/function.mysql-pconnect.php
Though, in my experience, I don't like this. If you're running your database and webserver on the same host, then the time to set up a connection is very VERY small. I initialize a connection for every visitor with minimal cost.
If the mysql database is remote relative to your website, you might want to consider this. Setting up a connection takes multiple round trips. So if you ping WEBSERVER to MYSQLSERVER and it's 100ms. It's going to take multiple times that to get a connection. In this case, it might be worth using a persistent connection.
I had never heard of persistent connections before, and I don't understand the advantages.
I run a PHP/MySQL based website, it receives tens of thousands of page views per day. In my header file on each of these pages I have just used mysql_connect() and I haven't bothered with terminating the connection in the footer file.
In my case are there any advantages of using mysql_pconnect()?
Using a persistent connection leaves the connection open after the script has finished executing. Opening and closing connections over and over causes overhead, while small, that will eventually mount up as the number of requests go up.
However, if you read the manual page for mysql_pconnect it states:
If PHP and MySQL are on the same server or local network, the connection time may be negligible, in which case there is no advantage to persistent connections.
If this is the case it may not be worth the trouble changing your code.
You can find more detailed information on persistent connections at the same site as above.
Check out this URL:
http://us3.php.net/manual/en/function.mysql-pconnect.php
Basically mysql_pconnect() tries to find a persistent connection already open with the credentials that you've specified. If it doesn't find one it makes a new one. It also doesn't close the connection after a statement is executed
So really in your case you may not notice a difference but in reality you should probably be using mysql_pconnect().
Are there any tricks to transferring and/or sharing the Resource Links across multiple PHP pages? I do not want the server to continue connecting to the database just to obtain another link to the same user in the same database.
Remember that the link returned from mysql_connect() is automatically closed after the script it originated on completes executing. Is there anyway to close it manually at the end of each session instead?
PHP allows persistent mysql connections, but there are drawbacks. Most importantly, idle apache children end up sitting around, holding idle database connections open. Database connections take up a decent amount of memory, so you really only want them open when they're actually being used.
If your user opens one page every minute, it's far better to have the database connection closed for the 59 seconds out of every minute you're not using it, and re-open it when needed, than to hold it open continually.
Instead, you should probably look into connection pooling.
One of the advantages of Mysql over other heavier-weight database servers is that connections are cheap and quick to setup.
If you are concerned about large numbers of connections to retrieve the save information, you may like to look at such things as caching of information instead, or as well as getting the information from disk. As usual, profiling of the number, and type of calls to SQL that are being made, will tell you are great deal more than anyone here guessing at what you should really be doing next.