Connecting PHP with MySQL DB in XAMPP issues - php

I'm trying to connect my PHP to my XAMPP DB server, but as it seems I'm not doing something right.
This is my code:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$db= "blogdata";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
And it returns:
Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'username'#'localhost' (using password: YES) in C:\xampp\htdocs\KungFu\Blog.php on line 8
Connection failed: Access denied for user 'username'#'localhost' (using password: YES)
I think everything is correct, tried different variations but as you can tell it is not working.

Best way to connect php to MySQL is to use new method : PDO :)
PHP :
$bdd = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', '');

Related

How to fix the error: "Connection failed: Access denied for user 'username'#'servername' (using password: NO)"

I am trying to implement a login system on my website and therefore I need to connect a php file to a mySQL database. One problem, I keep getting this error:
Connection failed: Access denied for user 'username'#'servername' (using password: NO)
Does anyone know how to fix this error? Or can anyone tell me what I'm doing wrong?
I know this question has been asked like a million times already, but none of those answers have worked for me. They all are solutions for a local server and database, where mine is at a webhost so i think this works different.
Here's my code:
<?php
$servername = "servername";
$dBUsername = "db username";
$dBPassword = "db passwordd";
$dBName = "db name";
$dBPort = "db port";
$conn = new mysqli($servername, $dBUsername, $dPassword, $dBName, $dBPort);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
The variable name you are using in the password parameter slot is not the same as the one you defined above ($dBPassword vs $dPassword):
$dBPassword = "db passwordd";
// ...
$conn = new mysqli($servername, $dBUsername, $dPassword, $dBName, $dBPort);
In PHP the undefined variable "$dPassword" will be treated as a NULL which is likely why you're getting that "using password: NO" in the returned error.

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!

MySqli Database connection access denied php

Hey I'm connecting to a DB in PHP like I normally would, the info is all there and correct. Checked the password a few times. Is there anything here that would ruin my connection? Here's my (nice and vague) error message:
Connection failed: Access denied for user 'user'#'localhost' to database 'DB'
<?php
header('Access-Control-Allow-Origin: *');
?>
<style>
</style>
<html>
<body>
<?php
$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "DB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
Connection failed: Access denied for user 'user'#'localhost' to database 'DB'
Nope thats not vague, its as accurate as it can get. User user does not have permission to access Database DB on localhost even if the password is correct. No if else or guesses or chances of it being incorrect in thinking so.
Look at database permissions for DB

Can't connect to database with 000webhost

I'm trying to learn php but when I attempt to connect to a mysql database, I get this error. I don't think anything is wrong with the code itself but perhaps there is an error on the hosts side? I'm using 000webhost so.
Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied
for user 'a7976620_db1user'#'localhost' (using password: YES) in
/home/a7976620/public_html/index.php on line 17
Here is my php:
<?php
$servername = "localhost";
$username = "a7976620_db1user";
$password = "******";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
In https://www.000webhost.com, there are two options in the cPanel:
Fix ownership and
Fix file permissions
You can apply this and it will be fine.

php establishing connection with mysql database

Hello everyone am trying to connect to MySQL database but getting this error:
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'#'localhost' (using password: YES) Error: Could not establish connection.Access denied for user 'root'#'localhost' (using password: YES).
Here is my code:
<?php
$host = "localhost";
$db = "resumedb";
$username = "root";
$password = "sa";
$phpConnect = mysqli_connect($host, $username, $db, $password);
//checking the if connection.
if($phpConnect === false)
{
die("Error: Could not establish connection.".mysqli_connect_error());
}
?>
The right syntax is:mysqli_connect("myhost","myuser","mypassw","mybd")
so try:
$phpConnect = mysqli_connect($host, $username, $password, $db);
refer this
Basically If you are the newbee in PHP so you should
1.first start the Xamp/Wamp service.
2.else db credentials for password should be $password = "".

Categories