Need help sorting out MySQL permissions and connections - php

So I'm trying to set up a PHP site that accesses and writes to a database. This should be relatively simple but I've become swamped with a mess of permission issues and I'm new to PHP so I'm not sure how to fix them all.
First off, this warning occurs when trying to access the database:
Warning: mysqli_connect(): The server requested authentication method unknown to the client [mysql_old_password]
The connection also fails due to this:
Warning: mysqli_connect(): (HY000/2054): The server requested authentication method umknown to the client
In my research I found that this warning was related to password hashing due to versioning of PHP (link)
However attempting to run the solution on that page gives me the following error:
Error Code: 1044. Access denied for user 'user'#'host' to database 'mysql'
And I don't know how to gain permissions in that database.
Here's my php code for reference (sensitive info removed):
$con=mysqli_connect("IP:port","user","password","database_name");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "Succesfully connected";
echo "<br />";
}
$result = mysqli_query($con, "SELECT * FROM OWNERS") or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo $row['email'] . " " . $row['type'];
echo "<br />";
}
Can someone help me out with this one? I know it's a lot but it seems like all of the issues are related and I'm not sure where to start with fixing them. (I'm using XAMPP to run the php and MySQL workbench to run queries)

I suppose the mysql is running on a different machine in your network. try this to see if it works:
CREATE USER 'user'#'IP' IDENTIFIED BY 'your password';
GRANT ALL ON database_name.* TO 'user'#'IP';
if the user is already in your system but with different IP then you should create it again using the IP of your mysql server. you could also try % instead of IP in create user and grant statements but it is not recommended in production.

Related

Failed to connect to MySQL: Access denied for user 'root'#'localhost' (using password: YES)

I was told by someone else that input this url https://beachbumbookworm.com/service.php into their browser it works. However, when I place the url in my google browser it will not work.
I have been working from a tutorial at this site https://codewithchris.com/iphone-app-connect-to-mysql-database/#app at 3.2 in the tutorial. There is a service.php file below that I pasted into text editor on my MacBook Air. I have made a table named locations in phpmyadmin, I have given proper permissions for the username and password. There aren't any characters in the password that would warrant a denial. I am using exactly what the tutorial is using, which includes a wordpress + Bluehost environment. From the bluehost there is a control panel I have access to and there, I have accessed phpmyadmin and mysqldatabases. After the service.php file was edited by me, I uploaded it into php per the article. I'm not sure what's wrong, please help.
<?php
// Create connection
$con=mysqli_connect("localhost","username","password","dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM Locations";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($con);
?>
Here is an image of the database table created
It doesn't matter what browser do you use in that case, because MySQL connection creates at the backend before sending the response;
At the line with mysqli_connect("localhost","username","password","dbname"); code try to replace credentials with that one, which you have used in PhpMyAdmin;
Check your MySQL credentials with your MacOS terminal. Use this command: mysql -u root -p

Newbie trying to learn how to connect to phpmyadmin

I have one file on one web serve and my phpmyadmin SQL databases on a seperate server and am currently trying to connect to my external serve but it doesnt seem to work.
I keep getting the error message:
'Access denied for user ''#'localhost' (using password: NO)'
and am not sure what this means.
Any help would be greatly appreciated or even an article to get started on figuring this out since I cant seem to find anything myself.
Thank you
EDIT --- PDO SCRIPT ---
My PDO Script is the generic one from w3, I have place holders for security reasons.
<?php
$servername = "externalIP";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=Reservations", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
you have to find your phpmyadmin page where you login using also a password.
Try this one :if you have not assign password into database for specific user then you just have to apply 'root' as user and leave blank password field and also set your hostname as 'localhost' or 127.0.0.1
also remember that you have to start first apache and mysql services before access.
If you are using remote server as database then not required.in that case you have to apply your remote Ip address and also remember that in that remote server if you have not specific user then leave password as blank. one another way to debug mysql connection into your remote server creating mysql connection file with its required paramter to check that your remote server ok or not.
This error will come most probably because of user permission issue. Make sure you have privilege to that user for that database.

PHP app not communicating with certain MYSQL databases

I have a PHP web app which has been working fine until today (it is sitting on an Windows/IIS server). When I attempt to access a page which connects to the MYSQL database, I get a blank page in firefox. In IE I get a 500 Internal Server Error. Normal PHP works fine, it is simply when I try to connect to the database. The mysqli connection is not giving any error messages.
The weird thing - I have phpmyadmin on the same server and it is working fine, I can see my database and interact with it.
I also have another database on the server which is working fine, the user attached to that database has read only access (as it is an archive database), but the PHP web app connecting to it works.
What have I tried?
Initially I thought it was a user issue, so I created a new user in phpmyadmin with full privileges and had the PHP app log on using it instead, same result.
I then thought that it was an issue with the database, so I copied the data and structure to a new database, same result.
The MYSQL.err log file has no errors and indicates that the server is accepting connections (which it is, as phpmyadmin is working).
I have even tried writing a test script:
<?php
//Debugging
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
include "settings.php";
echo $HOST . "<br>";
echo $DBNAME . "<br>";
echo $DBUSERNAME . "<br>";
echo $DBPASSWORD . "<br>";
$CON = new mysqli($HOST, $DBUSERNAME, $DBPASSWORD, $DBNAME);
//Check the connection and report error if it failed
if ($CON->connect_errnum > 0)
{
die('Unable to connect to database [' . $CON->connect_error . ']');
}
$result = $CON->query("select user() AS us");
if (!$result)
die("There was an error running the query [" .$CON->error ."]");
}
while($row = $result->fetch_assoc())
{
echo $row["us"] . "<br>";
}
?>
If I don't have he code from the $result = $CON->query("select user() AS us"); down, it displays the 4 values. Once I include it, I get the 500 Internal server error page.
I am at a loss to figure out what is happening or where to start looking.
One of my support classes had a syntax error in it, this caused the page to fail before it got to the database connection. Because my error reporting was turned off, I could not see this (even though I thought it was on). I had to go in to PHP.ini and set display_errors = off to display_errors = on. This then showed the error message.

PHP Access Denied Error

I have a login script, PHP based. when I run the page I get the following error:
Access denied for user 'usernameI'#'mydomainname.com' (using password: YES)
I've confirmed my password and that the respective user has all rights to the database. I tested a small connection script and get the same error. Here is the code:
<?php
$con = mysqli_connect("mydomainname.com","usernameI","mypassword");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
...I have the above within the body tag of the PHP page. Could I get some help as to what I'm doing wrong or if there is something I've overlooked or need to confirm? Thanks!

Trying to create MySQL database gives HTTP/404 The requested URL alias not defined

I'm having a small problem and am in need of a help from more experienced people.
I'm trying to create a MySQL database with PHP code, but I'm getting this message: "HTTP/404
The requested URL alias for C:/Apache24/htdocs/testsite_01/Error creating database: Access denied for user ''#'localhost' to database 'testbase_db' is not defined."
I have Apache, PHP and MySQL installed on my home PC and Apache and MySQL are started and running. I have tried checking Apache's httpd.conf file, but I'm quite new to this server stuff and am not sure if it's something in there I should change, or add to make the problem disappear.
I use the default Apache htdocs folder for the files and have a sub folder there called testsite_01 for this particular file. However, that shouldn't be the problem, as it gives the same message when moving everything to the default root folder and trying to run from there. I have tried Googling and reading quite lot of stuff here and elsewhere, but haven't found an answer so far.
This is how my PHP code looks like for creating the database.
function create_DB()
{
$con = mysqli_connect("localhost", "", "");
if (mysqli_connect_errno())
{
echo "Failed to connect to the database: " . mysqli_connect_error();
}
$sql = "CREATE DATABASE testbase_db";
if (mysqli_query($con, $sql))
{
echo "Database created succesfully";
}
else
{
echo "Error creating database: " . mysqli_error($con);
}
}
You need to connect to the database server/host on the machine, not a file.
$con = mysqli_connect("localhost", "", "");
I resolved the problem myself. Thanks for all the answers. This question can be removed.

Categories