I am getting errors in php - php

I am getting this error and I have done everything I can think about. Can someone help me out I have been working on this all day. I am still a little newbie at it. Thanks.
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'username'#'localhost' (using password: YES) in C:\wamp\www\member-form.php on line 81
error on connect
else{
$hostname="localhost";
$database="contact";
$mysql_login="username";
$mysql_password="password";
81.if (!($db = mysql_connect($hostname, $mysql_login , $mysql_password))){
echo "error on connect";
}
else{
if (!(mysql_select_db($databse, $db))){
echo mysql_error();
echo "<br>error on table connection";
}
else{
$SQL="Insert into tblUsers(username,password,firstname,lastname,email,address,city,state,zip, phone,signupDate)values)'".$_POST['username']."',PASSWORD('".$_POSR['password1']."'),'".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['address']."','".$_POST['city']."','".$_POST['state']."','".$_POST['zip']."','".$_POST['phone']."',NOW())";
mysql_query($SQL);
if (is_numeric(mysql_insert_id())){
header("Location:member-content.php?name=".$_POST['username']);
}
else{
echo "Sorry, there was an errot.Please try again ot contact the administrator";
}
mysql_close($db);//closeing out connection,done for now
}
}
}
?>

The mysql username or password in your some of your configuration file is incorrect. You should see what kind of code is on member-form.php line 81 as mentioned in the error.
Edit:
Create a new file test.php and put following code in it, and see in your browser what you get:
<?php
mysql_connect("localhost", "Web_User", "my1230") or die(mysql_error());
echo "Connected to MySQL<br />";
?>

This means you have set the wrong password. Check wherever you set your config that your password has the right case, and no typos.

Your password is wrong for the user you are trying to login as.
The default WAMP username is "root" and password is blank.
mysql_connect("localhost","root");

If you go into your mySQL control panel, you can add users with passwords, and then add them to databases. You need to create a user, and then add that user to the database with ALL privileges. If you are using OS X I strongly suggest Sequel Pro to help simplify.
http://www.sequelpro.com/
If you don't have OS X there are plenty of other tools such as:
http://www.phpmyadmin.net/home_page/index.php
Now, once you do that (created a user), take the username and password and plug them into your script. The username is the username you just created and the password is the password associated with that user.
The error message tells you that you couldn't connect.

Please don't use mysql it isn't save and it is deprecated use mysqli or pdo.
here below is an example for mysqli
$servername = "localhost";
$username = "root";
$password = "thisisthepassword";
$dbname = "exampledatabase";
$conn = mysqli_connect($servername, $username, $password, $dbname);

Related

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

I got stuck at this.
i am using hostinger for my website
$server = "localhost";
$user = ""; //Username
$password = ""; //Password
$database = ""; //Database
$connection = mysqli_connect($server,$user,$password,$database);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
This is my code.
i could not find a relevant answer.
Thanks in advance.
thanks for helping me.
it was hosting issue. i mailed to the support system of the hostinger.
they told me to change my database password and then update the script.
i follow the instructions and my error was solved.
Thank You All.
Are you sure that localhost is the right host?
I would also dump the mysqli_connect_errno() value because it returns the error code from the last connection attempt, which will give you a very good indication of where the issue is.
and
I would check if the user u201944136_root is defined and have permissions against localhost (mysql> SHOW GRANTS FOR 'u201944136_root'#'localhost';)

Access denied to mySQL database using PHP

So I've been trying to solve this problem for a couple of hours now. And I have to make something clear.
I can access the mySQL database from terminal using
mysql -u root
I can also access the database from mySQL workbench
My root user on mySQL has no password
Now my code online is pretty simple. It is PHP calling the mysqli
<?php
$database = "myDatabase";
// Create new connection
$conn = new mysqli("localhost", "root", "", $database);
if ($conn -> connect_error) {
die ("Something went wrong: " . $conn -> connect_error);
} else {
echo "It works";
}
?>
Now I always get the error code
Something went wrong: Access denied for user 'root'#'localhost' (using password: NO)
If I could get any help that would be greatly appreaciated. I've already tried to grant access but I think I'm doing it wrong.
mysqli_connect may be interpreting the empty password as no password at all
using password: NO
Either try to set a password for the root user, or create a new user with a password. And via Tripp Kinetics comment, make sure the permissions are set properly for the new user.

Warning: mysqli_connect(): (HY000/1044): Access denied for user ''#'localhost' to database 'dbtest' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
When i try to connect to my sql server, it gives me the error:
Warning: mysqli_connect(): (HY000/1044): Access denied for user ''#'localhost' to database 'dbtest'.
I just created the user and gave it all permissions, but i still dosent work. Before i used another account, but i couldn't change the database perssions for it. Anybody who have a potential fix?
Here is the connect code.
<?php
// this will avoid mysql_connect() deprecation error.
error_reporting( ~E_DEPRECATED & ~E_NOTICE);
// but I strongly suggest you to use PDO or MySQLi.
$DBHOST= "localhost";
$DBUSER= "testadmin";
$DBPASS= "";
$DBNAME= 'dbtest';
$conn = mysqli_connect(localhost, testadmin, $DBPASS, dbtest);
// $dbcon = mysql_select_db($DBNAME);
if ( !$conn ) {
die("Connection failed : " . mysql_error());
}
?>
I know this thread is old, but Ahmad Sharif is correct. The best way to quickly resolve this issue, especially if you're with a new hosting company, is to just create a new database using the "MySQL Database Wizard" in cPanel, then grant ALL privileges to everyone to ensure the remote access will work. Just make sure that ALL check boxes are checked while creating the new database from the "MySQL Database Wizard".
In my case grant all privileges to 'my_user' solved the problem
GRANT ALL PRIVILEGES ON *.* TO 'my_user'#'%' ;
You can check the following link for further information
https://mariadb.com/kb/en/grant/
There is no host, no user and no db-name in your mysqli_connect call. Do:
$conn = mysqli_connect($DBHOST, $DBUSER, $DBPASS, $DBNAME);
or
$conn = mysqli_connect('localhost', 'testadmin', $DBPASS, 'dbtest');
And if you wouldn't suppress E_NOTICEs, you'd see that php is looking for undefined constants.
Please check that you check all grant privileges. If you do not then delete the previous DB and create a new Db from Mysql Database Wizard (I guess it is shared hosting). In the last step, you can check all the privileges.
I have got the same error and I could solve my problem by grant all privileges.
I don't know if anyone had a solution to this I have just been trying to create a separate table for a list of emails with a same key column. I got exactly the same error message but solved it by writing a php file which i can run on the server.... The php file creates the table ! And it had permissions to let me write to it as well !
Here is the file i used to let the server make the table i needed. And let me access it from my other php files. I will call it here "LetServerMakeTable.php" ha! When i uploaded it to my 'ricky' heliohost server i ran it on the browser thus:
http://virowiz.heliohost.org/LetServerMakeTable.php
Result:... I had a php file which collecter email addresses and a used ID code and the wrote it successfully to my new Emails table within my database.
<html>
<head>
<?php
$conn = mysqli_connect("localhost", "virowiz_Kevin", "password", "virowiz_Covid3a");
// Check connection
if (!$conn) //<---- ! conn = NOT conn
{
echo "Failed";
die("Connection failed: " . mysqli_connect_error());
}
else
{
echo "Connected successfully";
}
//---------------------------------- Create the table.
$sql = "CREATE TABLE Emails
(
CVTRn BIGINT(12),
Email char(254),
Mobile char(13)
)";
//----------------------------------
mysqli_error($conn);
if (mysqli_query($conn, $sql))
{
echo "Table created successfully";
}
else
{
echo "Error creating table: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
</head>
</body>
</html>

mysql_connect(): Access denied for user 'adminryan'#'localhost' (using password: YES)

I'm very new to PHP and mySQL and I've been trying to create a logon via xampp with apache and mySql server. I keep on getting this error can someone explain what the error actually means? I've seen some people ask this question but the answer usually only pertains to their code especially. I'll provide my code as well. I've been watching this series of youtube tutorials if anyone is interested in what I have done http://www.youtube.com/watch?v=NuiTzSdGmKM . Hope someone can help thanks! My code below:
<?php
$username = "yolo";
$password = "swag";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");
$selected = mysql_select_db("login", $dbhandle);
$myusername = $_POST['user'];
$mypassword = $_POST['pass'];
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$query = "SELECT * FROM logon WHERE Username='$myusername' and Password='$mypassword'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
mysql_close();
if($count==1){
echo 'It worked!';
}
?>
The error is about establishing the connection to a mySQL server.
(User 'adminryan' can't connect to server 'localhost' and a password was used trying to connect.
This can have several reasons, like:
user adminryan doesn't exist.
the provided password is wrong.
the provided servername is wrong.
user adminryan doesn't have the proper privileges.
etc...
your code says:
$dbhandle = mysql_connect($hostname, $username, $password) or die("Could not connect to database");
so I wonder why you see the error given by you instead of "Could not connect to database".
I would expect mysql_connect would return FALSE and therefore die("Could not connect to database") would be executed. (And all code after die(...) wouldn't be executed.)
It has nothing to do with $query = ...
I can't, but I would vote down the comments of MuhammadAli and ArtisticPhoenix because they don't make any sense in relation to the error you provided.
And following your code this line won't be executed.
Do switch to PDO or at least mysqli though.
For the purposes of having a possible solution for others that encounter this error and confirm the username, password and database do exist. I was coming across the same error and when restructuring my requests I was able to get the desired result.
$username = 'username';
$password = 'password';
$db = mysql_connect('localhost', $username, $password);
mysql_select_db("database",$db);
In my instance I have the full username and database being used. So it seems to depend on your cpanel account username. So for example if my domain cpanel login was mydata, then my username would be mydata_username and my database would be mydata_database.
Now I don't know if that matters or not, I haven't tested it the other way around, I have had this error a few times and this is a solution that worked for me. I don't know why it worked as I'm no expert but it did and it's worth a try for others who are experiencing the same issue.
The only difference between this code and my earlier code which is similar to this error is my use of quotes. This code is using single quotes ', the earlier code used double quotes ".

How do I connect to mysql from php?

I'm working through examples from a book on php/mysql development.
I'm working on a linux/apache environment.
I've set up a database and a user. I attempt to connect with this line of code:
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
I get this error:
Warning: mysql_connect()
[function.mysql-connect]: Access
denied for user 'www-data'#'localhost'
(using password: YES) in
/var/www/hosts/dj/connect.php on line
3 unable to connect to database:
Access denied for user
'www-data'#'localhost' (using
password: YES)
I can only guess what is happening here:
I think www-data is a username for apache. Upon the database connection, the credentials being passed in to mysql are not those of my database user, but rather apache's own credentials. Is that what is happening here?
How do I pass in the credentials I've defined for my user ?
edit:
By the way - I do have credentials in the variables $db_hostname, $db_username, $db_password.
they are passed in by another file using require_once. If that file can't be found, then I get an error. So, I know that my username and password are being used by my script.
Both my scripts can be seen here:
http://pastebin.com/MUneLEib
#
Solved:
Thanks guys.
A couple of you pointed out that I had coded carelessy.
Also, I was particularly pleased by Neo's answer: he told me why the username of the owner of the apache process was being used.
:)
I just looked at your code! The variable with the username is $database_username but
you are using $db_username.. Change your code to:
$db_server = mysql_connect($db_hostname, $database_username, $db_password);
or you could change the line with username with:
$db_username='[your mysql user]';//or the username you created
When you don't pass anything it will be the user mysql assumes but it will not get the password so if you hadn't defined $db_password it would say: (using password:NO)
you set $database_username with you user but you are passing $db_username which is not set so the user is the linux username as default when nothing is passed with the password for the mysql user! Since there is no mysql user with that password or privileges or even with that name you are not given access!
That user is www-data which is as you guessed an apache user assigned to client-side requests!
In your login.php you use the variable $database_username, but in your connection function you use $db_username. Try matching them up.
The username goes into $db_username, and the password goes into $db_password.
All these other answers are so presumptuous, as if you don't know that $db_username means database username and same for password.
The error says that you've specified an username and password. You just specified the wrong ones. You need to use the username and password of MySQL, NOT the system username/password combination, so no, this will not be www-data. This may be root and some password, but again, these credentials are specified within MySQL, and are not (necessarily) the same as the system users and passwords.
Your MySQL installation should have a root user with a default password (which you should promptly change). There are several options: you can add an user via the MySQL command line or use an interface like cPanel or Webmin if your provider has something like this; I've used both of these and they both have easy interfaces to add new MySQL users and assign them privileges.
Also just a tip: I typically create one user per database and give the user full privileges on the database, and then use that user with the application linked to the database.
And then of course, once you create a MySQL user account and give it privileges on your web app's database, fill in that username and password into your script.
<?php
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
?>
Make sure you actually assign your database login username and password to those variables before you try to connect. For example, before the line with mysql_connect():
$db_username = 'myuser';
$db_password = 'mypass123';
I'd recommend you go through a tutorial regarding MySQL and PHP before trying to go any further - just so you understand how it all works.
Try this one:
http://www.tizag.com/mysqlTutorial/mysqlconnection.php
Also, documentation is your friend:
http://php.net/manual/en/function.mysql-connect.php
The error given could mean that the privilege has not been granted, or perhaps the password is incorrect. Check that user is created and granted access, or just issue this to be sure it set to allow access on the mysql server.
$ mysql -u root -p
password:
(blah blah blah from server)
GRANT ALL PRIVILEGES ON db_base.* TO 'db username'#'localhost' IDENTIFIED BY 'some password';
If all privileges is too much, consider giving only the basic permissions needed:
GRANT SELECT,UPDATE,DELETE ON db_base.* TO 'db username'#'localhost' IDENTIFIED BY 'some password';
By the way, the server response to this statement on success is Query OK, 0 rows affected.
Your code is correct, it is saying your password is incorrect so your mysql database is rejecting the mysql connection. If you are using xampp use:
localhost
as hostname &
root
as username & in xampp there is no password so it would be
$password = ""; then $dblink = new mysqli($hostname, $dbuser, $password, $dbname);
so you can set your vars like this
<?php
$hostname = "localhost";
$dbuser = "root";
$password = ""; // means there is no password to the database
$dbname = "test"
?>
Conclusion :
if you get this as your message -
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'www-data'#'localhost' (using password: YES) in /var/www/hosts/dj/connect.php on line 3 unable to connect to database: Access denied for user 'www-data'#'localhost' (using password: YES)
It means the password you entered is wrong or there is no password.
Stop using mysql_connect()!
You should not use this or other related functions related to this extension.
It's depreciated and removed in PHP 7.0.0 and beyond.
An alternative is using PDO or MySQLi.
Below is an example script for connecting to MySQL using PDO:
<?php
/* Connect to a MySQL database using driver invocation */
/* DSN options:
http://php.net/manual/en/ref.pdo-mysql.connection.php
Error handling options:
http://php.net/manual/en/pdo.error-handling.php
Learn how to secure against SQL injection attacks:
http://php.net/manual/en/pdo.prepared-statements.php
*/
$user = 'myusername';
$pass = 'mypassword';
$host = 'localhost';
$mydb = 'mydatabase';
$dsn = "mysql:dbname=$mydb;host=$host";
try {
$dbh = new PDO($dsn, $user, $pass);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>

Categories