clarification: is_resource()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

clarification: is_resource()

Post by raghavan20 »

example code:

Code: Select all

$conn = mysql_connect("$hostName", "$userName", "$password");

//should I use if ($result) or if (is_resource($result))
//which is the best of the two above?
//what is the difference between the two?
if (is_resource($conn)){
	$query = "select ...";
	$result = mysql_query($query);

	//should I use if ($result) or if (is_resource($result))
	//which is the best of the two above?
	if (is_resource($result)){
	          mysql_result($result, 0, "" );
	}
}
Q2. Wot is the difference btw @mysql_connect and mysql_connect?
Q3. Wot do u actually mean by having a permanent database connection when you use mysql_pconnect()?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Q1: A resource identifier is returned upon succesful connection or querying. Thus the function is_resource will identify a variable as a rescource or false.

Q2: As for the use of @ it is an error supressant. It will hide any errors that particular function may barf out.

Q3: I'll let the manual answer this
mysql_pconnect() acts very much like mysql_connect() with two major differences.

First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect()).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

@ is an error ignore/hide operator

pconnect isn't permanent, it's persistent. There's a difference.. basically, the connection is kept open for a period of time after the page stops processing. It will (hopefully) shorten the time required to get a new connection because it's already open on the next connection.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Thanks for your explanations both of you guys...
Q1: A resource identifier is returned upon succesful connection or querying. Thus the function is_resource will identify a variable as a rescource or false.
I do understand the meaning of it when i went thru in http://www.php.net. But do you recommend that always using a if(is_resource($result)) stmt is safe/ advantageous than using if($result)?
@ is an error ignore/hide operator
Do you mean that in a sample situation, if I am not able to connect to a database using mysql_connect() then the error message that normally echoes onto the screen is blocked/ hidden?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

raghavan20 wrote:Thanks for your explanations both of you guys...
Q1: A resource identifier is returned upon succesful connection or querying. Thus the function is_resource will identify a variable as a rescource or false.
I do understand the meaning of it when i went thru in http://www.php.net. But do you recommend that always using a if(is_resource($result)) stmt is safe/ advantageous than using if($result)?
using the is_* functions is always a good idea over not.. they will help avoid warnings, notices, and errors as you write more and more complex systems.
@ is an error ignore/hide operator
Do you mean that in a sample situation, if I am not able to connect to a database using mysql_connect() then the error message that normally echoes onto the screen is blocked/ hidden?
yes.
Post Reply