Jump to content

php include connection function


squib

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);

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.