php - Function file will not recognize included PDO database connection -


i have func.php file contains function gets user's details:

require_once 'connection.php';  function getui($username){      $query = "select * usernames username = ?";      $sth = $pdo->prepare($query);      $sth->bindvalue(1, $username);      $sth->execute();      $result = $sth->fetch(pdo::fetch_assoc);      return $result; }  

in connection.php have:

require_once 'details.php';  $pdo = new pdo("mysql:host=" . $dabhost . "; dbname=" . $dabname ."", $dabusern, $dabpassw); 

and in details.php have:

$dabhost = "localhost"; $dabname = "dab1"; $dabusern = "root"; $dabpassw = "root"; 

and ultimately, have userdetails.php has bunch of html code , displays results function getui() bring back. require func.php @ beginning:

require_once 'folder1/func.php';

my directory looks this:

rootfolder/userdetails.php rootfolder/folder1/details.php rootfolder/folder1/connection.php rootfolder/folder1/func.php 

the issue when open userdetails.php, error in php_error.log file says following:

php notice:  undefined variable: pdo in /rootfolder/folder1/func.php on line 58 php fatal error:  call member function prepare() on null in /rootfolder/folder1/func.php on line 58 

where if put code @ top of userdetails.php, work , bring expected results. therefore, there wrong how requiring files/scope of variables think...

could explain me doing wrong?

your appreciated!

thanks lot!

update:

passing $pdo connection second argument in function solved proble, unable retrieve x result returned $return variable, instance:

echo $result['date'];

it says variable $result not defined. idea why occurring?

thanks lot again!

when declare

function getui($username) {} 

$pdo not available because outside scope of function.

you'll need pass in additional parameter or find other mechanism getting $pdo inside getui().

if need more information


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -