PHP MySQL connection persistence -
i've read ton persistent database connections between php , mysql (mysql_connect vs. mysql_pconnect). same pdo , mysqli. it's lack of understanding on one, how can database connection persistent between webpages? in code:
$conn = mysql_pconnect( $server , $user, $pass ); mysql_select_db( $dbname );
if 2 users load page @ same time, 2 different $dbname variables, php make 1 connection database or two? that
$conn = mysql_connect( $server , $user, $pass );
would make 2 connections.
if pconnect reuses connection opened first user, mysql_select_db call work second user?
ideally, looking way have fewer database connections still able set default database in each php script. have clients use same php scripts, data stored in own client database (hence, $dbname different, mysql connection parameters same - same mysql ip address, user , password).
hope makes sense. can use mysql, mysqli or pdo, need know how accomplish best way without having possibility clients accidently write data else's database! in advance.
from reading of documentation , comments, see:
docs on mysql_pconnect (deprecated method)
second, connection sql server not closed when execution of script ends. instead, link remain open future use ( mysql_close() not close links established mysql_pconnect()).
and comment on page
persistent connections work cgi php managed fastcgi, contrary suggestion above work module version. that's because fastcgi keeps php processes running between requests. persistent connections in mode made immune connection limits too, because can set php_fcgi_children << mysql's max_connections <<< apache's maxclients. saves resources.
docs on mysqli_connect (new method)
prepending host p: opens persistent connection. mysqli_change_user() automatically called on connections opened connection pool.
docs mysqli_change_user:
changes user of specified database connection , sets current database.
so understanding follows: pconnect keeps connection open after script ends while process (or maybe group of processes) still alive (like in server fcgi set up). 1 script @ time uses connection, , when new script grabs connection user , database updated.
thus if use fcgi , persistent connections can reduce number of db connections open, scripts running simultaneously not sharing same connection. there no problem connection being confused database selected.
Comments
Post a Comment