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?