Page 1 of 1

default resource_link argument in mysql_error?

Posted: Sat Jul 12, 2008 2:25 am
by Apollo
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:

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 fine
But now watch this:

Code: 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!
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.

Re: default resource_link argument in mysql_error?

Posted: Sat Jul 19, 2008 5:37 pm
by Apollo
*gentle bump*

I'll rephrase the question, what do I have to fix here to make the following code:

Code: Select all

// show error of last opened SQL connection
$connection = null; // also tried false, 0, "", unset($connection), etc, nothing works?
print( mysql_error($connection) );
do the same as:

Code: Select all

// show error of last opened SQL connection
print( mysql_error() );
:?:

Anyone?

Re: default resource_link argument in mysql_error?

Posted: Sun Jul 20, 2008 11:54 pm
by Bill H
Your problem is you can omit the parameter, but if the parameter is included it must be a valid one. It must be a valid resource identifier, or the parens must be empty. Null or a null string is not the same thing as empty parens.

Re: default resource_link argument in mysql_error?

Posted: Mon Jul 21, 2008 5:17 am
by Apollo
Bill H wrote:Your problem is you can omit the parameter, but if the parameter is included it must be a valid one. It must be a valid resource identifier, or the parens must be empty. Null or a null string is not the same thing as empty parens.
Indeed, but doesn't PHP have some internal default value for this parameter?

If you make your own function with optional parameters, you have to define the default, e.g.:

Code: Select all

function MyFunction( $something, $bla=false, $somethingelse=0 )
{
 ...etc
}
Now you can call it with MyFunction("whatever"), omitting the last two params, which will in fact evaluate to MyFunction("whatever",false,0)

Since I don't have the function definition for mysql_error, I can't see what default parameter they use. Or is this really a different construction since mysql_error is a PHP built-in function? Is this a case of function overloading, just like in other languages?

Re: default resource_link argument in mysql_error?

Posted: Mon Jul 21, 2008 11:04 am
by Bill H
Probably an overload type of thing. You could do something like this:

Code: Select all

 
function GetMyGold( $connection=false )
{
   if ($connection == false)
          return mysql_query( "SELECT gold FROM rainbow WHERE leprechaun_name='Sniffles'" );
   else return mysql_query( "SELECT gold FROM rainbow WHERE leprechaun_name='Sniffles'" , $connection );
}
 

Re: default resource_link argument in mysql_error?

Posted: Mon Jul 21, 2008 12:51 pm
by RobertGonzalez
I think you hit it with:

Code: Select all

<?php
$error = $connection ? mysql_error($connection) : mysql_error();
?>