squib Posted March 26, 2014 Share Posted March 26, 2014 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... Quote Link to comment Share on other sites More sharing options...
gristoi Posted March 26, 2014 Share Posted March 26, 2014 require_once (__DIR__.'/../login/incDB.php'); Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted March 26, 2014 Share Posted March 26, 2014 (edited) 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 March 26, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
squib Posted March 26, 2014 Author Share Posted March 26, 2014 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... Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted March 26, 2014 Share Posted March 26, 2014 (edited) 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 User-defined Functions Function Arguments Returning Values and Variable Scope - Ignoring anything to do with globals Edited March 26, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
squib Posted March 26, 2014 Author Share Posted March 26, 2014 Thanks for the links Ch0cu3r. I ended up just putting everything into a class. Quote Link to comment 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.