default resource_link argument in mysql_error?

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
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

default resource_link argument in mysql_error?

Post 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.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: default resource_link argument in mysql_error?

Post 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?
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: default resource_link argument in mysql_error?

Post 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.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: default resource_link argument in mysql_error?

Post 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?
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: default resource_link argument in mysql_error?

Post 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 );
}
 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: default resource_link argument in mysql_error?

Post by RobertGonzalez »

I think you hit it with:

Code: Select all

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