0

I'm trying to connect to a MYSQL database using PHP. I have this to connect to the database:

<?php
$servername = "localhost";
$username = "orbital";
$password = "XXXXXXXX";
$dbname = "helios";

function establishConnection() {
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    return $conn;
}?>

However, whenever I call establishConnection() I get:

Connection failed: Access denied for user 'webmaster'@'localhost' (using password: NO)

webmaster is the user on my machine which runs Apache. Why is it using the webmaster user with no password instead of the specified 'orbital' user?

Rodrigo Taboada
  • 2,727
  • 4
  • 24
  • 27
Gregory Sims
  • 511
  • 7
  • 18

2 Answers2

2

You need to pass the DB variables to the function. Do not use the global keyword; pass them as parameters.

<?php
$servername = "localhost";
$username = "orbital";
$password = "XXXXXXXX";
$dbname = "helios";

function establishConnection($servername, $username, $password, $dbname) {
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    return $conn;
}?>
Dave
  • 3,658
  • 1
  • 16
  • 9
0

Those variables are out of the function's scope. You should make those variables parameters to the function:

function establishConnection($servername, $username, $password, $dbname) {
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    return $conn;
}
$myCon = establishConnection($servername, $username, $password, $dbname);

OR:

You could declare them global variables in the function (as below), although many people are against it (see the comments):

<?php
$servername = "localhost";
$username = "orbital";
$password = "XXXXXXXX";
$dbname = "helios";

function establishConnection() {
    // Create connection
    //without the following line, this function has no access to any
    // of these variables because they are outside the function scope
    global $servername, $username, $password, $dbname;
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    return $conn;
}?>
developerwjk
  • 8,619
  • 2
  • 17
  • 33
  • 2
    I'd steer clear of `global`'ing the variables to add them to the function scope. Rather passing them as arguements - `function establishConnect($servername, $username, $password, $dbname) { }`. Read more about [Why global state is the devil](http://tomnomnom.com/posts/why-global-state-is-the-devil-and-how-to-avoid-using-it). – Darren Feb 17 '15 at 00:31
  • That's a likely cause, as the variables will essentially be null values. – Zarathuztra Feb 17 '15 at 00:31
  • 2
    I have to agree with @Darren, `global` is pretty much never the way to go. Though to be clear, it would likely solve the issue, its just that its a *bad solve*. – prodigitalson Feb 17 '15 at 00:31
  • I also agree with ditching global – Zarathuztra Feb 17 '15 at 00:32
  • @Darren, At your link there (http://tomnomnom.com/posts/why-global-state-is-the-devil-and-how-to-avoid-using-it) none of those examples even uses the `global` keyword. They all use `$GLOBALS` or are examples of using class members in a global fashion inside a class, and thus have NOTHING to do with this at all. – developerwjk Feb 17 '15 at 00:42
  • @Zarathuztra, Updated the answer to show both ways. – developerwjk Feb 17 '15 at 00:43
  • 1
    And its doubtful a beginner having an issue with variable scope is using dependency injection or has any plans to any time soon. – developerwjk Feb 17 '15 at 00:50
  • I'm up-voting as this is a double-edged sword. If used correctly, `global` will do as intended, I just don't want the OP to go around `global`'ing everything. ([And the answers here explain why](http://stackoverflow.com/q/1557787/2518525)) – Darren Feb 17 '15 at 00:52