Jump to content

Variables in include file not accessible from script


mgasparel

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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.