Php / pdo / msql - access denied

There is a little pdo problem that I have been working on for a while. Since I do not know what is wrong here, I thought about taking him to this list. Perhaps some of you know more ...

I have a website with a login that verifies the user and password for a mysql based database. When the pdo connection is done in one file, everything works fine, you can log in without any problems. Just as it should work ...

However, when moving part of the database connection to a separate function that I include from another file, pdo fails and gives me:

SQLSTATE [28000] [1045] Access denied for user '...' @ '...' (using password: none) Fatal error: call prepare () member function for non-object in /.../ .. . / ... on line 41

For clarity, here is the code:

Version 1:

It works:

<?php require "./vars_and_functions.php"; /* open database connection */ try { $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass); /* query */ $query = "SELECT uname, passw FROM members WHERE uname = ? AND passw = ?"; $q = $pdo->prepare($query); $q->execute(array($u_name, $p_word_md5)); $result = $q->rowCount(); if($result == 1) { /* we have a match */ /* close the database connection */ $pdo = null; /* and redirect */ header("..."); } /* if */ else { /* wrong credentials */ /* close the database connection */ $pdo = null; /* and go back to the login page */ header("..."); } /* else */ } /* try */ catch(PDOException $e) { echo $e->getMessage(); } /* catch */ ?> 

Here is version 2

This does not work:

 <?php require "./vars_and_functions.php"; /* open database connection */ $pdo = database_connection(); /* query */ $query = "SELECT uname, passw FROM members WHERE uname = ? AND passw = ?"; $q = $pdo->prepare($query); $q->execute(array($u_name, $p_word_md5)); $result = $q->rowCount(); if($result == 1) { /* we have a match */ /* close the database connection */ $pdo = null; /* and redirect */ header("..."); } /* if */ else { /* wrong credentials */ /* close the database connection */ $pdo = null; /* and go back to the login page */ header("..."); } /* else */ } /* try */ catch(PDOException $e) { echo $e->getMessage(); } /* catch */ ?> 

My includefile vars_and_functions.php looks like this:

 $db_host = "..."; $db_name = "..."; $db_user = "..."; $db_pass = "..."; function database_connection() { try { $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass); } catch(PDOException $e) { echo $e->getMessage(); } return $pdo; } 

The only real difference from my mind is that here the pdo connection is made through a function call, while the function is in the included file vars_and_functions.php.

What is wrong here?

+6
source share
2 answers

Your database_connection() function does not receive connection variables in the correct scope, so they are not set when you try to connect and therefore are passed as NULL , and PDO by default sets the connection host to localhost .

Pass them as function parameters:

 // Defined at global scope... $db_host = "..."; $db_name = "..."; $db_user = "..."; $db_pass = "..."; // Pass the 4 variables as parameters to the function, since they were defined at global // scope. function database_connection($db_host, $db_name, $db_user, $db_pass) { try { $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass); } // Called as: $pdo = database_connection($db_host, $db_name, $db_user, $db_pass); 

If you use only these variables inside the connection function and do not need them elsewhere, consider their definition in the scope of the function, which will save you from passing them as parameters.

 function database_connection() { // Only needed here, so define in function scope $db_host = "..."; $db_name = "..."; $db_user = "..."; $db_pass = "..."; try { $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass); } 

The ultimate and often least desirable option is to define the variables in the global scope as you did, but access them via $GLOBALS[] (or the global ) in a function:

 function database_connection() { try { $pdo = new PDO("mysql:host={$GLOBALS['db_host']};dbname={$GLOBALS['db_name']}", $GLOBALS['db_user'], $GLOBALS['db_pass']); } 

Note that if you are working with error_reporting and display_errors , as it should be, you will see undefined variable notifications.

 error_reporting(E_ALL); ini_set('display_errors', 1); 
+3
source

In addition to Michael Berkowski's answer, you can also pass the global keyword like this:

 function database_connection() { global $db_host, $db_name, etc; // your code here } 

See http://php.net/manual/en/language.variables.scope.php for more information on variable scope in PHP.

0
source

All Articles