default resource_link argument in mysql_error?
Posted: Sat Jul 12, 2008 2:25 am
With the MySQL functions like mysql_query, mysql_fetch_array, etc. you can give an optional link identifier, this is a resource of the SQL connection to be used. If not specified, it uses the last opened connection by default. This also works for mysql_error and mysql_errno.
For mysql_query, mysql_fetch_array etc I can just use false, but this doesn't work for mysql_error and mysql_errno.
Example:
But now watch this:
The last line gives a PHP error: supplied argument is not a valid MySQL-Link resource
I can overcome that by doing (in ShowError) $error = $connection ? mysql_error($connection) : mysql_error();
My question: what can I specify as link_identifier argument to make it use the last connection? (besides not specifying it at all)
I've tried false, 0, '' (empty string), null, an unset()'ed variable, but it all results in the error above. It's not a big problem as I have a workaround, but just wondering.
For mysql_query, mysql_fetch_array etc I can just use false, but this doesn't work for mysql_error and mysql_errno.
Example:
Code: Select all
function GetMyGold( $connection=false )
{
return mysql_query( "SELECT gold FROM rainbow WHERE leprechaun_name='Sniffles'" , $connection );
}
$myConnection = mysql_connect(...);
mysql_select_db ("Leprechauns" , $myConnection ); // $myConnection can be omitted here
GetMyGold($myConnection); // works fine
GetMyGold(); // also works fineCode: Select all
function ShowError( $connection=false )
{
$error = mysql_error( $connection );
print("Uh oh, something bad happened: $error");
}
ShowError($myConnection); // works fine
ShowError(); // does NOT work!I can overcome that by doing (in ShowError) $error = $connection ? mysql_error($connection) : mysql_error();
My question: what can I specify as link_identifier argument to make it use the last connection? (besides not specifying it at all)
I've tried false, 0, '' (empty string), null, an unset()'ed variable, but it all results in the error above. It's not a big problem as I have a workaround, but just wondering.