| | |
Variable scope/declaration/assignment query
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Nov 2005
Posts: 9
Reputation:
Solved Threads: 0
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: Without I get four of these notices that I'm using the variables undeclared: then: 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!
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:
•
•
•
•
Parse error: syntax error, unexpected T_VAR in [correct but v long path!] on line 8
•
•
•
•
Notice: Undefined variable: dbhost in [long path] on line 16
•
•
•
•
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
[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!
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]
[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]
If I've been a help please confirm by clicking the Add to Lafinboy's Reputation link in the header of this reply.
Lafinboy Productions
:: Website Design :: Website Development ::
Lafinboy Productions
:: Website Design :: Website Development ::
![]() |
Similar Threads
- Is it a problem due to scope or compiler ? (C++)
- Thread pausing and shared variable synchronization? (C#)
- JFrame GUI Problem (Java)
- HELP! What does this mean.......? (Visual Basic 4 / 5 / 6)
- please help with mysql_num_rows(): (MySQL)
- C++ Syntax (C++)
Other Threads in the PHP Forum
- Previous Thread: howto: expire cookie at end of session?
- Next Thread: PHP IP Log
| Thread Tools | Search this Thread |
ajax apache api array arrays beginner binary broken cache cakephp checkbox class cms code confirm cron curl customizableitems database date display dynamic echo email error external file files folder form forms forum function functions google header headmethod howtowriteathesis href htaccess html iframe image include insert integration ip java javascript joomla limit link login loop mail malfunction menu method mlm multiple mysql neutrality oop paypal pdf php phpmysql play problem query question radio random recursion regex remote root script search select server sessions sms soap source space sql syntax system table tutorial update upload url validator variable video web xml youtube





