Db connection

I just downloaded an example (http://www.dynamicajax.com/fr/JSON_AJAX_Web_Chat-271_290_324.html) that contains this code:

 function db_connect($server = 'your server (usually "localhost")', $username = 'your username', $password = 'your password', $database = 'web chat database', [B]$link = 'db_link'[/B]) {

what is $link? I have connected many times with JSP, I don’t need this parameter… what is $link, please…

thank you…

can you show the code for the full function please.

you can’t attach files in these fora??? ok, here it is…

http://mayacove.com/dev/database.txt

this is entire code – untouched by me – of database.php in downloaded example cited in my OP…

thank you…

Sounds like a resource (i.e. the database connection), but normally that’s something the function would return, rather than have passed it.

Maybe if it get’s passed it, the function avoids connecting again and just returns the existing connection resource?
No way of knowing without seeing its code.

but I just posted entire code for the db connection…

http://mayacove.com/dev/database.txt

thank you…

Ah, I see your code now. I loaded this page before you responded. It sets the connection resource as a variable in the global scope. The parameter is asking what that variable should be called in the global scope.

Using the argument default $db_link will be defined.

$$ is a variable variable

e.g.


$orange = 'What a great colour';

$var_name = 'orange';

echo $$var_name; //What a great colour

function db_connect($server = 'your server (usually "localhost")', $username = 'your username', $password = 'your password', $database = 'web chat database', $link = 'db_link') {
    global $$link;

    $$link = mysql_connect($server, $username, $password);

    if ($$link) mysql_select_db($database);

    return $$link;
  }

$link is the name of the variable that the db connection is assigned to. when it is used as $$link the extra dollar sign means to look for the variable with the name that is in the value of $link. So basically if $link = ‘db_link’ typing echo $$link is the same as typing $dblink.

The global command just makes the variable accessible (and editable) within the function.

The reason they are doing this is in case you want to create a variable with a name other than $db_link.

If you don’t need to alter the variable name you could just amend the code above to:

function db_connect($server = 'your server (usually "localhost")', $username = 'your username', $password = 'your password', $database = 'web chat database') {
    global $db_link;

    $db_link = mysql_connect($server, $username, $password);

    if ($db_link) mysql_select_db($database);

    return $db_link;
  }

This just removes $link from the code. Does that help?

yes this worked… thank you very much… :slight_smile: