Jump to content

Recommended Posts

Hey there everyone, I'm having issues with an include file for database connections. I've got a function in an include file that connects to a mySql db. It works fine with a file in the same directory, but not in a different directory. Seems like a scope issue, as it is able to find the include file in both cases, but I haven't been able to figure it out yet :).

Thanks in advance

/login/incDB.php

  
          function getConn() {
  		global $conn;
  		
  		//connect to mysql
  		$host = 'localhost';
  		$dbUsername = 'username';
  		$dbPassword = 'password';
 		$db = 'db';
 		$port = 3306;
 		 
 		$conn = new mysqli($host, $dbUsername, $dbPassword, $db);
 		
 		if($conn->connect_errno > 0) {
 		    die('Unable to connect to database [' . $conn->connect_error . ']');
 		}
 	}
 
 

/login/index.php

include_once 'incDB.php';
    
    getConn();

$sql = "call getLoginInfo ('$username','$password');";

if(!$result = $conn->query($sql)){
     die('There was an error running the query [' . $conn->error . ']');
    } else {
echo 'logged in fine';
}
/upload/index.php

require_once ('../login/incDB.php');
//include_once ('../login/incDB.php');

function insertInfoToDB($year, $month, $day, $userUID) {
        //global $conn;
        getConn();
        
        $sql = "call insertInfo ('$year', '$month', '$day', '$userUID');";
        mysqli_query($conn, $sql);
        
        mysqli_close($conn);
    }

//error for mysqli_query($conn, $sql);
Notice: Undefined variable: conn in...
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in...

//error for mysqli_close($conn);
Notice: Undefined variable: conn in...
Warning: mysqli_close() expects parameter 1 to be mysqli, null given in...
 
Link to comment
https://forums.phpfreaks.com/topic/287297-php-include-connection-function/
Share on other sites

Globals should not be used at all. You should be passing $conn as a function argument

 

So your getCon function should be coded like

          function connectDB($host, $user, $pass, $db) {
 		 
 		$conn = new mysqli($host, $user, $pass, $db);
 		
 		if($conn->connect_errno > 0) {
 		    die('Unable to connect to database [' . $conn->connect_error . ']');
 		}

 		return $conn; // return the mysqli object
 	}
 

Then when you want to connect to the database, you pass this function the db credentials

//connect to mysql
$host = 'localhost';
$dbUsername = 'username';
$dbPassword = 'password';
$db = 'db';
$port = 3306;

$conn = connectDB($host, $dbUsername, $dbPassword, $db); // connect to db, capture the returned mysqli instance/object

And the capture the returned mysqli instance/object on calling the function

 

Any function whereby you need to interact with the database, you should pass $conn as a function argument and not as global, eg your insertInfoToDB function should be coded like

// define the mysqli object as function argument
function insertInfoToDB($conn, $year, $month, $day, $userUID) {
        
        $sql = "call insertInfo ('$year', '$month', '$day', '$userUID');";
        mysqli_query($conn, $sql);
        
        mysqli_close($conn);
    }

Then when calling the function it'll be

$conn = connectDB(...);


// when calling function, pass it the mysqli object
insertInfoToDB($conn, $year, $month, $day, $userID);
Edited by Ch0cu3r

Thanks guys.  

 

Ch0cu3r, I had tried this earlier but was having issues with getting the variables.  I've got them in the include file as well.  I've tried putting them both in the getConn function and just leaving them on the outside.  In both cases I don't seem to be able to get the data in them.  

 

Just for SAG, I tried the following in the include file...

        function getHost() {
		$host = 'localhost';
		return $host;
	}
	
	$host = 'localhost';

getHost() came back no problem, while $host did not...

Notice: Undefined variable: host...

Just for SAG, I tried the following in the include file...

        function getHost() {
		$host = 'localhost';
		return $host;
	}
	
	$host = 'localhost';

getHost() came back no problem, while $host did not...

Notice: Undefined variable: host...

 

That would be the expected behaviour.

 

When you use  return  it only returns the variables value, not the variable itself. I recommend you to read up on the following

Edited by Ch0cu3r
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.