0

I have an html form on my site and I am trying to process the data and then store it in a mysql database on my server. In order to connect securely to mysql via php, I have, as suggested here, placed the login information in a configuration file outside of the main webroot.

The code executes seamlessly when it is written like this:

<?php
$mysqli = mysqli_init( );
$mysqli->options(MYSQLI_READ_DEFAULT_FILE, "/this/is/the/filepath/to/my.cnf");
$mysqli->real_connect(NULL, NULL,'*********',NULL );  

if (mysqli_connect_errno()) {
    printf("Connect failed: ", mysqli_connect_error());
    exit();
}

$email = $mysqli->real_escape_string($_POST['email']);

$mysqli->query("INSERT INTO subscribetest (email) VALUES ('$email')");  
?>

But when I take the password out, like this:

<?php
$mysqli = mysqli_init( );
$mysqli->options(MYSQLI_READ_DEFAULT_FILE, "/this/is/the/filepath/to/my.cnf");
$mysqli->real_connect(NULL,NULL,NULL,NULL ); 

if (mysqli_connect_errno()) {
    printf("Connect failed: ", mysqli_connect_error());
    exit();
}

$email = $mysqli->real_escape_string($_POST['email']);

$mysqli->query("INSERT INTO subscribetest (email) VALUES ('$email')");
?>

The code crashes and I am given the error message: 'Warning: mysqli::real_connect(). Access denied for 'user'@'webaddress.net' (using password:NO)'

Why can my php script read everything EXCEPT the password from the config file? It is able to read the username, host, and database from the config file; the password is the only one which is left out. I've looked everywhere, spent hours on this, but I haven't been able to find any solutions. Has anyone else had this problem? What can I do?

1
  • 2
    As the error states you need to check the connection. You are passing NULL for everything do you have a valid user with no password ?
    – Deepak
    Sep 3, 2012 at 2:34

2 Answers 2

1

The documentation says that setting the password parameter in real_connect() to NULL means "user does not need a password". Does it work if you call it without any parameters (they're all optional)?

7
  • I've tried calling it without any parameters, and I get the same error that I get when all parameters are set to NULL (aka read from the config file) except for the password. You referenced the php documentation for real_connect(); the mysql documentation for real_connect (dev.mysql.com/doc/refman/5.0/en/mysql-real-connect.html) says that you can instruct real_connect() to read the parameters from an option file by setting those parameters to NULL: "it is possible to have the value taken from an option file rather than from an explicit value in the mysql_real_connect() call." Sep 3, 2012 at 13:54
  • Alright, so there's probabyly something wrong with your options file. Can you post it?
    – Simon
    Sep 3, 2012 at 15:04
  • The configuration file reads as follows: '[client] host = host.com user = newsletter password = ******** database = ed_newsletter' I know that the password and hostname are correct because when I place them in the real_connect() call explicitly (that is, write them in the php code), the MySQL statement executes correctly. Sep 3, 2012 at 15:28
  • Odd. If you have a shell on the server, does running mysql w/o any arguments log you in? Can you change the password to ASCII only characters to test whether PHP has a problem parsing the options file (maybe quoting the password helps, like password="blahblah")?
    – Simon
    Sep 3, 2012 at 20:30
  • Better yet, can you create an empty options file and add line by line to see if PHP is offended by one?
    – Simon
    Sep 3, 2012 at 20:37
0

This bug still seems to exist: https://bugs.php.net/bug.php?id=43812

Workaround:

$iniData = file_get_contents('/etc/mysql/debian.cnf');
$iniData = preg_replace('/#.*$/m', '', $iniData);
$mysqlConfig = parse_ini_string($iniData, true);
$mysqli = new mysqli( $mysqlConfig['client']['host'], $mysqlConfig['client']['user'], $mysqlConfig['client']['password'] );
if ( $mysqli->connect_error ) {
    die(sprintf("MySQL Connect Error: %s", $mysqli->connect_error));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.