Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql variables connect error 1

Status
Not open for further replies.

Rached

Programmer
Mar 30, 2003
54
DK
Hi all, i would appreciate alot if any one can guide me through this issue.
In our VPS, using plesk, i am hosting a domain.biz. I created subdomain.domain.biz and an ftp, mysql user for that account. I gave the subdomain mysqluser full privilleges on subdomaindB. In the subdomain.domain.biz i have a php file that connects to subdomaindB, with the hostname, dBname, username, and pass as global variables, now when i call a function inside the php file to connect to the dB, using these variables, i get:
"Access denied for user: 'apache@localhost' (Using password: YES)"
But if in the function, i use normal text as parameter, it works fine. Any idea ?

example (does not work):
<?php
$host="localhost";
$user="username";
$password="password";
$dbName = "subdomaindB";

function checkUser($userID)
{
$db = mysql_connect($host,$user,$password);
if (!$db) exit(mysql_error());
$w = mysql_select_db($dbName);
if (!$w) exit(mysql_error());
$sql = "";
$sql="any sql statement";
$result = mysql_query($sql,$db);
mysql_close();
$num_rows = mysql_num_rows($result);
mysql_free_result($result);
return $num_rows;
}
?>

example (this works)
<?php
$host="localhost";
$user="username";
$password="password";
$dbName = "subdomaindB";

function checkUser($userID)
{
$db = mysql_connect("localhost","username","password");
if (!$db) exit(mysql_error());
$w = mysql_select_db("subdomaindB");
if (!$w) exit(mysql_error());
$sql = "";
$sql="any sql statement";
$result = mysql_query($sql,$db);
mysql_close();
$num_rows = mysql_num_rows($result);
mysql_free_result($result);
return $num_rows;
}
?>
 
try this:
function checkUser($userID)
{
global $host,$user,$password;
$db = mysql_connect($host,$user,$password);
if (!$db) exit(mysql_error());
$w = mysql_select_db($dbName);
if (!$w) exit(mysql_error());
$sql = "";
$sql="any sql statement";
$result = mysql_query($sql,$db);
mysql_close();
$num_rows = mysql_num_rows($result);
mysql_free_result($result);
return $num_rows;
}


Known is handfull, Unknown is worldfull
 
thanks vbkris, your suggestion worked, and i will be using it for now, nevertheless, i do not understand why i have to re-declare global variables in every single function, and that contradicts the priciple of having variables declared once globally.. quite annoying.
 
Variable in PHP are local in scope unless declared as global in a function.

See
You can also use the superglobal array $GLOBALS in your functions to refer to variables declared outside of your function.

Ken
 
Suggestion:
Instead of assigning the username, host etc. to a variable I would recommend to define them as constants. They are not manipulated by the script, so there is no need for a variable. The defined constant makes them available regardless of scope:
Code:
define('DB_HOST','myserver.me.com');
define('DB_USER','myuser');
define('DB_PASS','myPass');
function myFunction($myParam){
  mysql_connect(DB_HOST, DB_USER, DB_PASS);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top