Which username/password do I use to connect to mySQL - php

So, I'm SUPER new to mySQL. Having issues connecting to my database with PHP. Hoping someone can point me in the right direction.
I can login to our Cpanel using my username/password. Using the web gui I was able to create a database.
Now when I try to connect to the database (or even just the server for that matter) using PHP I get an error:
Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'username'#'servername' (using password: YES) in /home/ropepart/public_html/techportal/sandbox/mysqltest.php on line 8
Connection failed: Access denied for user 'username'#'servername' (using password: YES)
To me, it seems like this is a username/password error. But I an using the same username/password that I use to login to the web gui. Why can I successfully login there, but can't login with PHP?
Here's my code (taken pretty much directly from W3Schools):
<?php
$servername = "servername";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
(Hopefully it's obvious that I've changed my servername/username/password for the purposes of this post)

Check the details correctly, By deafult
host = localhost
username = root
password = (No password leave it as empty)
database = (your database)
<?php
$connection = new mysqli('host','username','password','your_database');
if($connection->connect_error || $connection->error){
echo "error";
}else{
echo "done";
}
?>

you should add database name.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "demo"
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
//if you are using xampp you shoul locate this file you get information from this
passwords.txt

MySQL database username/password is not necessarily same as cPanel username & password.
Here is what you should do:
Login to your cPanel
From dashboard click "MySQL® Databases"
Add a new user from there (https://prnt.sc/kpiby9). Just fill username
and password (note down the password).
Add that user with your database by selecting both from the dropdown
(https://prnt.sc/kpidqx)
Now, use this username & password to connect with the database.
mysqli_connect(serverhostname,username,password,dbname);

Related

PhpMyAdmin connection

I am trying to connect my Database on PhpMyAdmin with my webPage. However I keep getting this error:
Connection failed: Access denied for user 'root'#'localhost' (using password: NO)
My code (php) is:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_client2";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
I do not understand what is wrong:
[enter image description here][1]
[1]: https://i.stack.imgur.com/vYBYy.png
Hope you can help me, thank you in advance.
Please enter a password to the phpcode $password = "";
phpmyadmin disabled root login by default.
If you want to allow root login you will have to update /etc/phpmyadmin/config.inc.php and set
$cfg['Servers'][$i]['AllowRoot'] = TRUE;
Also mysql change the ability to login from root out of the shell.
please see this link https://askubuntu.com/questions/763336/cannot-enter-phpmyadmin-as-root-mysql-5-7
The recommendation is to create an account other than root and grant all privileges.

Connect online mysql database with the local xampp server using php

I tried using the following code in php:
<?php
$servername = "www.psgcirdask.com";
$username = "xxx"; //hidden for security purpose
$password = "password";
$dbname = "psgcilqh_calibration";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM brands";
$result = $conn->query($sql);
$conn->close();
?>
I got an error:
Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'xxx'#'157.51.147.43' (using password: YES) in C:\xampp\htdocs\justshawarmapos\checkthedatabase.php on line 9
Connection failed: Access denied for user 'xxx'#'157.51.147.43' (using password: YES)
But i use the same username and password to connect to my database in my website.I am getting this problem only when i connect using local server.
You have to enable remote access in your mysql database.
Check this link
For hostgator
Check if all the infos provided are correct especially the servername then try to figure out if you have the right permission to access that database with that user!

What user and password in phpmyadmin will I use in cpanel?

This is my very first time uploading a website. now I'm having a problem with my hosting which is CPANEL.
I'm using the code
<?php
date_default_timezone_set("Asia/Manila");
$servername = "localhost";
$username = "root";
$password = "";
$db = "exampledb";
// Create connection
$conn = new mysqli($servername, $username, $password,$db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
for my database connection.
Now my problem is what code should I put in servername? instead of localhost?
and same to the username root and password?
On the hosting site in cpanel they normally give you the server name that you can use, the password you will need to create a db user also on cpanel, this will be your user name and password.
// localhost is fine or else you need to keep your server IP.you can find in cpanel.
$servername = "localhost";
// if you are using database in some other server you should provide IP
// you need to create a user with required privileges then need to provide username/password
// root also fine but better create a user
$username = "root";
$password = "";
// Here you need to mention which DB you want use for your application.
$db = "exampledb";

MySQL won't connect once web page goes live

Hey guys I'm new to MySQL and am having a little trouble with getting it to load. I have it all set up and running in my localhost page but as soon as I upload the folder to my server it says connection failed using password"yes". Idk why this is happening and have spent the past couple of hours trying to solve it but google has not been much help.
I created a database and used php to access that db. I have tried switching local host to a domain, to an ip address, and everything else i could think of but nothing has worked. It only works in the development localhost page.
Any advice is greatly appreciated.
edit-
This is the code
<?php
$servername = "localhost";
$username = "rudy_dps";
$password = "******";
$dbname = "******";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, qual, name FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Qualifications</th><th>Name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["id"]."</td><td>".$row["qual"]."</td><td> ".$row["name"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
and this is the error i get
Connection failed: Access denied for user 'rudy_dps'#'localhost' (using password: YES)
What exactly the message say? My initial thought on the error is that it would say:
Connection failed: Access denied for user 'XXXX'#'XXXX'
(using password: YES)
Which can mean only one of two things:
Your password is incorrect, or
Your user doesn't have privileges to connect to that database.
There is a mismatch in the configuration of your mysql.
Execute the following command in your database
GRANT ALL PRIVILEGES ON . TO BY 'USER'#'%' IDENTIFIED BY 'PASSWORD'
then
FLUSH PRIVILEGES

How to connect to phpmyadmin database?

I'm trying to connect to a database in phpmyadmin. I'm new to the process and unsure how to connect to it.I'm using godaddy to host. I have this line of code:
$db = mysqli_connect("https://.....secureserver.net", "...", "....", "authenticationdb_");
I went to the table on phpmyadmin and copied the url, then copied the username (the first "...") and password (the second "...") that I used to login in to godaddy, and the name of the database is authenticationdb_.
This is not working and I'm not sure why. I was unsure if the username and password were the ones that I used to login in to godaddy but I don't know what else they would be since i accessed phpmyadmin thru godaddy.
<?php
$servername = "localhost";
$username = "username"; //your user name for php my admin if in local most probaly it will be "root"
$password = ""; //password probably it will be empty
$databasename = ""; //Your db nane
// Create connection
$conn = new mysqli($servername, $username, $password,$databasename);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Categories