Ordinary_Shepp Posted April 3, 2014 Share Posted April 3, 2014 (edited) Hello ! I am new in php coding. So if you please help me in this regards I'll be very happy. I have received some error while connection to my mqsql database to a php file. At first I post my code.... "login.php" <?php $db_hostname = 'localhost' ; $db_database = 'PC' ; $db_username = 'admin' ; $db_password = 'maruf' ; ?> "func.php" <?php require_once('C:\xampp\htdocs\PC\login.php') ; function connect(){ $db_server = mysql_connect( $db_hostname , $db_username , $db_password ) ; if( !$db_server )die ( "Unable to connect to MySql : ".mysql_error()) ; mysql_select_db( $db_database ) or die("Unable to select database: ".mysql_error()) ; } function close(){ mysql_close() ; } function division(){ $mydata = mysql_query("SELECT * from division order by zeocode"); if( !$mydata ) echo "Database connection failed. ".mysql_error().'<br>' ; while( $record = mysql_fetch_array($mydata)){ echo '<option value ="' . $record['$name'] . '">' . $record['$name'] . '"</option>'; } } ?> "form.php" <?php require_once 'func.php'; connect() ; ?> <!DOCTYPE html> <html> <head> form </head> <body> <header> <h1 align='center'>Census</h1></header> <p> <select name ="division"> <?php query() ?> </select> <?php close() ?> </p> </body> </html> It shows me the following error . calling from the browser -> http://localhost/PC/form.php Notice: Undefined variable: db_hostname in C:\xampp\htdocs\Population_Census\func.php on line 10 Notice: Undefined variable: db_username in C:\xampp\htdocs\Population_Census\func.php on line 10 Notice: Undefined variable: db_password in C:\xampp\htdocs\Population_Census\func.php on line 10 Notice: Undefined variable: db_database in C:\xampp\htdocs\Population_Census\func.php on line 12 Unable to select database: No database selected then I change form.php into this <?php //require_once('C:\xampp\htdocs\Population_Census\login.php') ; $db_hostname = 'localhost' ; $db_database = 'PC' ; $db_username = 'admin' ; $db_password = 'maruf' ; function connect(){ $db_server = mysql_connect( $db_hostname , $db_username , $db_password ) ; if( !$db_server )die ( "Unable to connect to MySql : ".mysql_error()) ; mysql_select_db( $db_database ) or die("Unable to select database: ".mysql_error()) ; } function close(){ mysql_close() ; } function division(){ $mydata = mysql_query("SELECT * from division order by zeocode"); if( !$mydata ) echo "Database connection failed. ".mysql_error().'<br>' ; while( $record = mysql_fetch_array($mydata)){ echo '<option value ="' . $record['$name'] . '">' . $record['$name'] . '"</option>'; } } ?> which show me exactly same problem. Edited April 3, 2014 by Ordinary_Shepp Quote Link to comment https://forums.phpfreaks.com/topic/287487-undefined-variable-db_hostname-in-cxampphtdocs/ Share on other sites More sharing options...
Solution mac_gyver Posted April 3, 2014 Solution Share Posted April 3, 2014 php functions, properly, have local variable scope. the only variables that existing inside a function are those that are created/defined in that function and those that are passed into the function as call time parameters. Quote Link to comment https://forums.phpfreaks.com/topic/287487-undefined-variable-db_hostname-in-cxampphtdocs/#findComment-1474773 Share on other sites More sharing options...
Ordinary_Shepp Posted April 3, 2014 Author Share Posted April 3, 2014 Thanks, I do understand now. actually I come from C++ in php. So I thought global variables are accepted. Quote Link to comment https://forums.phpfreaks.com/topic/287487-undefined-variable-db_hostname-in-cxampphtdocs/#findComment-1474777 Share on other sites More sharing options...
boompa Posted April 3, 2014 Share Posted April 3, 2014 If you were regularly using global variables in C++, you were doing that wrong too. Quote Link to comment https://forums.phpfreaks.com/topic/287487-undefined-variable-db_hostname-in-cxampphtdocs/#findComment-1474781 Share on other sites More sharing options...
Ordinary_Shepp Posted April 3, 2014 Author Share Posted April 3, 2014 Sorry , I didn't mention I regularly use global variables in C++....... Quote Link to comment https://forums.phpfreaks.com/topic/287487-undefined-variable-db_hostname-in-cxampphtdocs/#findComment-1474818 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.