Page 1 of 1

clarification: is_resource()

Posted: Sun Aug 07, 2005 5:22 pm
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()?

Posted: Sun Aug 07, 2005 5:45 pm
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()).

Posted: Sun Aug 07, 2005 5:48 pm
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.

Posted: Sun Aug 07, 2005 5:56 pm
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?

Posted: Sun Aug 07, 2005 6:15 pm
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.