agente_secreto
agente_secreto

Reputation: 8079

Strange problem with mysql_connect and mysql_selectdb not taking some variables as arguments

I was working in some code in a PHP course I assisted, in a LAMP environment, and worked without problems. After the course, I took my code home and ran it on a WAMPServer, and for some reason, mysql_connect is not accepting some variables as arguments. I know this because I tried giving the parameters to mysql_connect by hand and the connection works. Also, I echoed all four variables and they print out OK.

I am new to PHP, but this doesnt make any sense to me.

The code:

functions.php

 <?php

require ("config.php");
     function conectardb () { 
     mysql_connect ($server, $login, $pass);
     mysql_select_db ($db);
 }

?>


config.php

<?php
 $login = "root" ;
 $pass = "" ;
 $server = "localhost";
 $db = "testing";
?>

I've tried replacing the plain arguments by their variables one by one, and found out the code works until this point:

 function conectardb () { 
     mysql_connect ($server, "root", $pass);
     mysql_select_db ("testing");
 }

If I use $login or $db, the connection will not work. Any ideas? This is driving me crazy.

Upvotes: 1

Views: 2206

Answers (4)

Brad Christie
Brad Christie

Reputation: 101614

Your problem is variable scope.

Unlike most languages, php doesn't automatically know about variables defined outside of a function. To access these you need to specify them as either global $server, $pass, $db; within the function, or use the $_GLOBALS[] variables.

Please see:

  • PHP Variable Scope (See how PHP manages scope)
  • $_GLOBALS (Global variables, which permits visibility within functions)
  • define() (For global constants, which would have visibility within functions)

Upvotes: 2

Risto Novik
Risto Novik

Reputation: 8295

@David Kuridža I recommend to not use mysql_ components and use mysqli or PDO extensions which of these two are included by default.

Upvotes: 0

David Kuridža
David Kuridža

Reputation: 7207

require ("config.php");
function conectardb () 
{ 
    global $server, $login, $pass, $db;
    mysql_connect ($server, $login, $pass);
    mysql_select_db ($db);
}

This should do the trick since variables are defined in a different scope. However, I recommend passing requires values as parameters.

function conectardb ($server, $login, $pass, $db) 
{ 
    mysql_connect ($server, $login, $pass);
    mysql_select_db ($db);
}

Upvotes: 2

Dan Grossman
Dan Grossman

Reputation: 52372

This is a misunderstanding of scope in PHP.

Functions exist in their own scope. You defined $login, $pass, etc. in a different scope than the function. The function can't access them, it can't see them.

You can do a couple things about this:

  1. Define the database connection parameters as constants instead of variables
  2. Define the variables within the function (requiring the configuration file inside the function will have the same effect)
  3. Use the global keyword to pull them from the global scope into the function scope

i.e.

$bar = 'hello world';
function foo() {
    global $bar;
    echo $bar;
}

Upvotes: 2

Related Questions