944,110 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3214
  • PHP RSS
Dec 3rd, 2005
0

Variable scope/declaration/assignment query

Expand Post »
Running PHP v5.0.4 (and I'm a newbie to php)

I am setting up functions for the database and the only bit of code in the file that works is the bit that is commented out! I've tried the variables with "var " in front to declare them explicitly, I've taken the var out again. I've moved the variables to another file (which is where I want them to be in an ideal world) and tried with and without but get the same error there.

I thought anything declared at the top of the file like that would automatically be global but added them as parameters for the function just in case and still get errors. The errors are:

With explicit declaration:
Quote ...
Parse error: syntax error, unexpected T_VAR in [correct but v long path!] on line 8
Without I get four of these notices that I'm using the variables undeclared:
Quote ...
Notice: Undefined variable: dbhost in [long path] on line 16
then:
Quote ...
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO) in [long path] on line 16
Error connecting to mysql
Please help.

[PHP]var $dbhost = 'HOSTNAME';
var $dbuser = 'USERNAME';
var $dbpass = 'PASSWORD';
var $dbname = 'DBNAME';

function db_open()
{
global $db_conn;
$db_conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
//$db_conn = mysql_connect("HOSTNAME", "USERNAME", "PASSWORD") or die ('Error connecting to mysql');
//mysql_select_db("DBNAME");
}[/PHP]If I swap the comments around the function works just fine so I hope someone can see what's going wrong when I pass the db values in as parameters.

Thankyou!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sybil is offline Offline
9 posts
since Nov 2005
Dec 4th, 2005
0

Re: Variable scope/declaration/assignment query

The variables used inside a function have scope local to the function. To reference variables defined outside the function you will need to reference them in the global scope inside the function. So this should work for you:
[PHP]$dbhost = 'HOSTNAME';
$dbuser = 'USERNAME';
$dbpass = 'PASSWORD';
$dbname = 'DBNAME';

function db_open()
{
global $db_conn, $dbhost, $dbuser, $dbpass, $dbname;
$db_conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
}[/PHP]
Reputation Points: 16
Solved Threads: 7
Junior Poster
Lafinboy is offline Offline
166 posts
since Jul 2004
Dec 4th, 2005
0

Re: Variable scope/declaration/assignment query

Thankyou - that works!

I thought I had understood that part of the php docs but I guess not
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sybil is offline Offline
9 posts
since Nov 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: howto: expire cookie at end of session?
Next Thread in PHP Forum Timeline: PHP IP Log





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC