1

I am getting the following error when using SHIFTEDIT IDE to try connect to my amazon EC2 instance running LAMP server and mysql server.

The code I am writing in PHP to connect to my sql server is as followed:

<?php
function connect_to_database() {

$link = mysqli_connect("localhost", "root", "test", "Jet");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);

}
?>

OUTPUT: Success: A proper connection to MySQL was made! The my_db database is great. Host information: Localhost via UNIX socket Access denied for user ''@'localhost' (using password: NO)

I am definitely using the right password for root as I can successfully login when using Phpmyadmin, I just cannot make a connection with PHP for some reason.

Currently, I have a single Amazon ec2 instance with LAMP server and a MySQL server installed. Any help will be much appreciated.

EDIT: I am using Php 5.6.17

rkk
  • 21
  • 2
  • 8
  • Which php version you are running there? as the mysqli PHP library, when using PHP 5.5 or higher. – Shashank Shah Feb 29 '16 at 18:39
  • What happens if you try to login as a different user? Create one and try it if you don't have any other users yet. – S.A Feb 29 '16 at 18:42
  • Just to be on the safe side: Can you please insert some output like `echo 'connection closed';` right after the `mysqli_close($link);` line and show us what the output is then (and also edit the code snippet, so that code and output are in sync)? – VolkerK Feb 29 '16 at 18:52
  • @ShashankShah Hi, I am using Php 5.6.17 – rkk Feb 29 '16 at 18:58
  • @SageArslan Hi, I have tried logging in as various other users - I get the same/similar error messages – rkk Feb 29 '16 at 18:58
  • @VolkerK Hi, when I add that line I get the following output: Success: A proper connection to MySQL was made! The my_db database is great. Host information: Localhost via UNIX socket connection closedAccess denied for user ''@'localhost' (using password: NO) So the message is being displayed :s – rkk Feb 29 '16 at 19:00
  • Meaning: The code that causes the `Access denied for user ''@'localhost' (using password: NO)` is _outside_ of the scope of the code you've posted. Which isn't too astonishing, given that the function name is `connect_to_database()`. Probably some other code expects this function to _return_ a (hopefully) valid and connected mysqli instance. But your function instead _closes_ that connection. – VolkerK Feb 29 '16 at 19:14
  • Your database is "localhost". Are you running this php on amazon EC2 instance? – Tin Feb 29 '16 at 19:21
  • @Tin Hi, Yes I am running php on amazon EC2 – rkk Feb 29 '16 at 19:26
  • Usually if you see "Access denied for user ''@'localhost' (using password: NO)" error, that means you're trying to access DB without giving password. In your code, it has been given. So I don't know :( – Tin Feb 29 '16 at 19:33
  • @VolkerK Hi, Im not sure what you mean by this.. Do you have a skype where I can private message you? I am new to this + stack overflow... I removed the function and just have the connection code and a SQL query to enter data into a table now. I am no longer getting the error message but when I run my query through PHP data is not added into the table so I guess theres still some permissions error? cheers :) – rkk Feb 29 '16 at 19:36
  • Possible duplicate of [MySQL ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)](http://stackoverflow.com/questions/10299148/mysql-error-1045-28000-access-denied-for-user-billlocalhost-using-passw) – Brooks Feb 29 '16 at 19:44
  • @Rajan Kerai , sorry no, I gave up skype for exactly that reason ;-) But maybe my example code gets you started... – VolkerK Feb 29 '16 at 20:06

1 Answers1

0

When you create a mysqli instance (either by new mysqli(...) or mysqli_connect(....) within a function/method, you have to take php's variable scope into account. Return the mysqli instance from the function and let the caller use and/or assign that instance.

<?php
/*
builds and throws an exception from the error/errno properties of a mysqli or mysqli_stmt instance
@param useConnectError true: use connect_error/connect_errno instead
@throws mysqli_sql_exception   always does
*/
function exception_from_mysqli_instance($mysqli_or_stmt, $useConnectError=false) {
    // see http://docs.php.net/instanceof
    if ( !($mysqli_or_stmt instanceof mysqli) && !($mysqli_or_stmt instanceof mysqli_stmt) {
        // see docs.php.net/class.mysqli-sql-exception
        throw new mysqli_sql_exception('invalid argument passed');
    }
    else if ($useConnectError) {
        // ok, we should test $mysqli_or_stmt instanceof mysqli here ....
        throw new mysqli_sql_exception($mysqli_or_stmt->connect_error, $mysqli_or_stmt->connect_errno);
    }
    else {
        throw new mysqli_sql_exception($mysqli_or_stmt->error, $mysqli_or_stmt->errno);
    }

}
/* creates a new database connection and returns the mysqli instance
@throws mysqli_sql_exception   in case of error
@return valid mysqli instance
*/
function connect_to_database() {
    $link = new mysqli("localhost", "root", "test", "Jet");
    // see http://docs.php.net/mysqli.quickstart.connections
    if ( $link->connect_errno) {
        // a concept you might or might not be interested in: exceptions
        // in any case this is better than to just let the script die
        // give the other code components a chance to handle this error  
        exception_from_mysqli_instance($link, true);
    }

    return $link;
}

try { // see http://docs.php.net/language.exceptions
  // assign the return value (the mysqli instance) to a variable and then use that variable
  $mysqli = connect_to_database();

  // see http://docs.php.net/mysqli.quickstart.prepared-statements
  $stmt = $mysqli->prepare(....)
  if ( !$stmt ) {
    exception_from_mysqli_instance($stmt);
  }
  ...
}
catch(Exception $ex) {
  someErrorHandler();
}

and a hunch (because of the actual error message; trying to use the default root:<nopassword> connection, that's the behaviour of the mysql_* functions, not of mysqli):
Do not mix mysqli and mysql_* functions.

VolkerK
  • 95,432
  • 20
  • 163
  • 226