What part of my code is getting me a warning? [duplicate] - php

This question already has answers here:
MySQL Error: : 'Access denied for user 'root'#'localhost'
(28 answers)
Closed 2 years ago.
The code
index.php file
<?php
include_once 'includes/dbh.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$sql = "SELECT * FROM data";
$result = mysqli_query($conn, $sql);
$datas = array();
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$datas[] = $row;
}
}
//print_r($datas);
//foreach ($datas[0] as $data) {
// echo $data['text']." ";
//}
?>
</body>
</html>
includes file
<?php
$server = "localhost";
$username = "";
$password = "";
$database = "test";
$conn = mysqli_connect($server, $username,
$password, $database);
if (!$conn) {
die("Connection failed: " .
mysqli_connect_error());
}
The Warning
Warning: mysqli_connect(): (HY000/1045): Access denied for user ''#'localhost' (using password: NO) in C:\xampp\htdocs\databasetoarray\includes\dbh.php on line 8
Connection failed: Access denied for user ''#'localhost' (using password: NO)
Other info
The database and php files are all correctly named.
The Goal
I am trying to get an array out of a database using a tutorial I found on the internet.
The Questions
Does this method even work if using correct coding? If so what can I change to make it work?

The default password for mysql is root if you want it to set another you should change it.

Please connect to localhost/phpmyadmin and verify the credentials and update it here:
$server = "localhost";
$username = "";
$password = "";
$database = "test";
By default the username will be root and password will be blank.

on localhost you shold use username is root, password blank and correct database name use following code
$server = "localhost";
$username = "root";
$password = "";
$database = "test";
$conn = mysqli_connect($server, $username,
$password, $database);
if (!$conn) {
die("Connection failed: " .
mysqli_connect_error());
}

Related

I can't start a session and can't login it?

When I want to start a session, I take this error. Also when I want to check email and password is right, I take this error. I'm new the Php (this is my first project) and I'm using Mysql for database. Can you help me about this situation?
Error like this:
Warning: session_start(): Failed to read session data: files (path:
D:\Program Files\xampp\tmp) in C:\Users\Mustafa
KANLI\eclipse-workspace\Hotel\Customer\login_1.php on line 2
Warning: Undefined array key "email" in C:\Users\Mustafa
KANLI\eclipse-workspace\Hotel\Customer\login_1.php on line 4
Warning: Undefined array key "password" in C:\Users\Mustafa
KANLI\eclipse-workspace\Hotel\Customer\login_1.php on line 5
<?php
session_start();
include("../src/database/connect_db.php");
$email = ($_POST['email']);
$password = ($_POST['password']);
$emailError = "Email is required";
$passwordError = "Password is required";
if($_POST){
$selectSql = $conn -> prepare("SELECT id, email, password FROM `customers`
WHERE email = '".$email."'");
//$selectSql -> execute(['eMail' => $email]);
$user = $selectSql->fetch(PDO::FETCH_ASSOC);
if($user){ // if user exist
if($password === $user['password']){ //login successful
$_SESSION['id'] = $user['id'];
echo "Successful";
echo "Successful";
//header("Location:index.php");
}
}
}
?>
This is the connect_db.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db_name = "hotelparadis";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $db_name);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
I moved project folder into the xampp/htdocs and it was fixed, thanks for your helping and suggestions.

Connection error MySQL

I copied this from W3Schools and when I try to run it it gives me the a error. I am really new to MySQL so I am trying to fix this but I don't know how.
ERROR:
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user
'username'#'localhost' (using password: YES) in
C:\xampp\htdocs\Informatica\test.php on line 10 Connection failed:
Access denied for user 'username'#'localhost' (using password: YES)
<html>
<body>
<?php
$servername = "localhost";
$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";
?>
</body>
</html>
your username or password is incorrect if you are very new and just starting with the local server try
$username = "root";
$password = "";
as your username and password
W3schools don't know your localhost details, do the following
$servername = "localhost";
$username = "root"; //put your username here
$password = ""; // your password here
Leave password empty and put "root" in username, that's the default xampp setting.
you have to add correct information provided by your hosting provider here :
$servername = "localhost";
$username = "username";
$password = "password";
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
//Connection to mysql check
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysqli_select_db($conn,'databaseName');
?>

Warning: mysqli_connect(): (28000/1045): Access denied for user 'mobileto'#'localhost' (using password: NO)

I am trying to access a PHP database I created online using this URL
http://dns.com/data_api/connect.php
I have created the database and it has no username and password attached to it at the point of creation. When I try to check if the connect.php could establish a connection to the database I get this error
Warning: mysqli_connect(): (28000/1045): Access denied for user 'mobileto'#'localhost' (using password: NO) in /home/mobileto/public_html/data_api/connect.php on line 11
Failed to connect to MySQL: Access denied for user 'mobileto'#'localhost' (using password: NO)
This is the connect.php file
<?php
$username = "";
$password = "";
$hostname = "localhost";
$dbase ="mobileto_holo";
//connection to the database
//$dbhandle = mysql_connect($hostname, $username, $password)
$con=mysqli_connect($hostname,$username,$password,$dbase);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// or die("Unable to connect to MySQL");
//echo "Connected to MySQL<br>";
//$conn = mysql_connect("localhost","root","");
?>
Please what could be wrong?
No Username or Password? :o That's impossible. Try giving root as the default username, instead of passing an empty string.
$username = "root";
$password = "";
$hostname = "localhost";
$dbase = "mobileto_holo";

Connection failed: Access denied for user ''username'#'localhost' (using password: YES)

I am trying to connect to MySQL on the server but gives following error. User is created and granted all privileges.
It's working on local machine but not after deploying to a server.
Also mysql_connect function works but new mysqli() gives access denied as mentioned below.
Also Tried adding port.
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user ''usernam'#'localhost' (using password: YES) in /home/domainname/public_html/autopublish/test.php on line 8
Connection failed: Access denied for user ''username'#'localhost' (using password: YES)
<?php
$servername = "localhost";
$username = "'xxxxx";
$password = "xxxxx";
$dbname = "xxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password); **//line 8**
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "no connection";
}
$sql = "SELECT * FROM pages";
$result = $conn->query($sql);
$data = array();
$arr = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
}
} else {
echo "No Token";
}
print_r(json_encode($data));
$conn->close();
?>
Either the user does not have the required rights for the database or the password is wrong.
Otherwise remove the user and use the following statement to create:
1.CREATE USER 'user'#'localhost' IDENTIFIED BY '123456789';
2.
3.GRANT ALL PRIVILEGES ON project.* TO 'user'#'localhost' WITH GRANT OPTION;
or try this:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
//Create
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $dbname);
echo "Yaaay";

Access denied for user ''#'localhost' (using password: NO) - trying to get an array

im trying to create a script that retrieves information from it, like article.php?id=1 returns a book and article.php?id=2 returns a computer. to accomplish this im trying to make an array with the table in it and i think my code is correct. i have my database "article" and the table "articles". and this is my code:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// create connection
$conn = new mysqli($servername, $username, $password);
// check connection
if ($conn->connect_error){
die("connection failed: " . $conn->connect_error);
}
$query = "SELECT * FROM articles";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
echo $row['name'];
?>
You mix mysqli_* API and mysql_* API. Use only mysqli_*
Change your code with this:
<?php
$username="youruser";
$password="yourpassword";
$database="yourdbname";
mysql_connect(localhost,$username,$password);
#mysql_select_db($database)
or die( "Impossibile selezionare il database.");
?>

Categories