Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Will mysql_connect itself?

General discussions related to php

Moderators: macek, egami, gesf

Will mysql_connect itself?

Postby chan15 » Tue Oct 09, 2012 5:25 pm

I write one function use mysql_connect named a, if I executed a() in index.php three times at the same time, will it detect it's already connected above and skip the connect process when second and third execution?
chan15
New php-forum User
New php-forum User
 
Posts: 3
Joined: Tue Oct 09, 2012 5:21 pm

Re: Will mysql_connect itself?

Postby Jeroen » Tue Oct 09, 2012 7:28 pm

No, it wont, if you call mysql_connect 3times, it connects 3times.
Jeroen
New php-forum User
New php-forum User
 
Posts: 40
Joined: Fri Aug 10, 2012 4:37 pm

Re: Will mysql_connect itself?

Postby chan15 » Tue Oct 09, 2012 7:42 pm

Can I use any code to detect it?
chan15
New php-forum User
New php-forum User
 
Posts: 3
Joined: Tue Oct 09, 2012 5:21 pm

Re: Will mysql_connect itself?

Postby seandisanti » Wed Oct 10, 2012 9:40 am

yes, just assign the connect to a variable, and then isset($connection) will determine if it's set.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Will mysql_connect itself?

Postby tkdbb84 » Tue Oct 23, 2012 11:09 am

An alternative would be to create the connection in the script that calls a(); and pass it as an argument to a, then you can handle the connection in the parent structure.

Code: Select all
//do upper parent stuff
$pdo_handle = new PDO('my connection string',$arg1,$arg2);
//do more stuff
$my_important_stuff = a($lala,$pdo_handle);
//process
$my_other_stuff = a($lala2,$pdo_handle);
//blahblah

function a($args_i_need, &$pdo_connection){
   $my_prepared_statement = $pdo_connection->prepare("SELECT * FROM myTable;");
   //do stuff
   return $my_results;
}


seems an easier way to control connections.
tkdbb84
New php-forum User
New php-forum User
 
Posts: 137
Joined: Tue Mar 08, 2011 9:25 am

Re: Will mysql_connect itself?

Postby chan15 » Tue Oct 23, 2012 6:52 pm

Thanks you all guys :)
chan15
New php-forum User
New php-forum User
 
Posts: 3
Joined: Tue Oct 09, 2012 5:21 pm


Return to PHP General

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron