Jump to content

Undefined variable: db_hostname in C:\xampp\htdocs\


Go to solution Solved by mac_gyver,

Recommended Posts

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 by Ordinary_Shepp
  • Solution

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.