mgasparel Posted June 14, 2011 Share Posted June 14, 2011 I am trying to use an include file to house my MySQL connection... but it doesn't seem to be accessible to my scripts which include it. conns.php <?php $dbAddress='localhost'; $dbName='omitted'; $dbUser='omitted'; $dbPass='omitted'; $conn = new PDO('mysql:host=' . $dbAddress . ';dbname=' . $dbName, $dbUser, $dbPass); $username = 'omitted'; $stmt = $conn->prepare("SELECT passhash FROM user_credentials WHERE email=?"); if ($stmt->execute(array($username))) { while ($row = $stmt->fetch()) { $hashedpass = $row[0]; } } echo $hashedpass; /* check connection */ if (mysqli_connect_errno()) { echo "Connect failed: %s\n", mysqli_connect_error(); exit(); } ?> authenticate.php include 'conns.php'; function getPasswordForUser($username) { $hashedPass=""; $stmt = $conn->prepare("SELECT passhash FROM user_credentials WHERE email=?"); //Line 20 if ($stmt->execute(array($username))) { while ($row = $stmt->fetch()) { $hashedpass = $row[0]; } } return $hashedpass; } If I navigate to conns.php, the script runs fine and displays the hashed password. If I run getPasswordForUser() from authenticate.php I receive the error: Fatal error: Call to a member function prepare() on a non-object in /home/nohype5/public_html/labs/secureLogin/authenticate.php on line 20 Why isn't my connection accessible from authenticate.php? Link to comment https://forums.phpfreaks.com/topic/239297-variables-in-include-file-not-accessible-from-script/ Share on other sites More sharing options...
gizmola Posted June 14, 2011 Share Posted June 14, 2011 Yes, for functions you need to either pass the variables in as parameters or declare them global. Link to comment https://forums.phpfreaks.com/topic/239297-variables-in-include-file-not-accessible-from-script/#findComment-1229346 Share on other sites More sharing options...
mgasparel Posted June 14, 2011 Author Share Posted June 14, 2011 Beautiful! Very new to php, my intuition on variable scope was completely wrong. I fixed the code by declaring my connection variable $conn as global, so it references the global-scope variable from within the function. function getPasswordForUser($username) { $hashedPass=""; global $conn; $stmt = $conn->prepare("SELECT passhash FROM user_credentials WHERE email=?"); if ($stmt->execute(array($username))) { while ($row = $stmt->fetch()) { $hashedpass = $row[0]; } } return $hashedpass; } many thanks Link to comment https://forums.phpfreaks.com/topic/239297-variables-in-include-file-not-accessible-from-script/#findComment-1229349 Share on other sites More sharing options...
gizmola Posted June 14, 2011 Share Posted June 14, 2011 Sure, not the recommended solution, but it is an option. Since it's an object, it would be much better to pass it in as a parameter. With php5 all objects are passed by reference. The other more OOP solution to this is to use the registry pattern, where you have a singleton registry object that you can get access to wherever it is needed, and typically things like configuration data, and database resources would be added to it, and thus can be made available inside functions, with a static method call. Zend framework has an implentation you can look at just to get an idea: http://framework.zend.com/manual/1.11/en/zend.registry.using.html Link to comment https://forums.phpfreaks.com/topic/239297-variables-in-include-file-not-accessible-from-script/#findComment-1229371 Share on other sites More sharing options...
mgasparel Posted June 14, 2011 Author Share Posted June 14, 2011 Thanks for the recommendations. I am passing the parameter for now, but I will look into the idea of using some sort of registry object. Link to comment https://forums.phpfreaks.com/topic/239297-variables-in-include-file-not-accessible-from-script/#findComment-1229375 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.