Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have been trying to connect to a mysql database many different ways but no matter how I do it I get an error. I'm pretty new to php so it might be my mistake but I would appreciate any help.
<?php
$servername = "localhost";
$username = "xyz";
$password = "1234567";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
All of the information is correct but it returns Connection failed: Access denied for user 'xyz'#'localhost' (using password: YES)
mysqli_connect($serverName,$Username,$Password,$Database);
Try appending your database name to the final parameter
Solutions:
Have you granted your user access to the database schema?
Have you ensured you're using the correct password/user?
Have you ensured your server name is correct? (Not localhost,
possibly an external IP?)
A possible solution would be try the server IP, as DNS can sometimes fail. Incase you was unaware, localhost IP is 127.0.0.1
Put localhost into quotes:
mysql_connect('localhost',$username,$password);
The error message Access denied for user 'xyz'#'localhost' (using password: YES) is an indication that either the password is wrong or the user xyz has not been installed as descibed in the manual: https://dev.mysql.com/doc/refman/5.6/en/adding-users.html
The username and password you specified is not registered as a database administrator.... the default username is root and password is blank
basically you did not set up permission correctly, double check host.
run
select user,host from mysql.user;
to check what is host address you provided
Related
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>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm making a website that uses SQL and PHP functionalities. How do I connect to a database?
I would advise you begin by looking here.
You need to ensure that you have created user credentials with the correct permissions to query the database before you try this. You can do this through the cPanel of your web server (I'm going to assume you are using a web hosted server for this question).
Once you have a working and tested connection to the database, you can then start looking at the mySQLi documentation here. Which will show you how to execute and retrieve results from a database query and how to handle the returned data with PHP.
I see you are seriously downvoted.
I learned it the hard way and I am still learning to post here.
Stack sites are supposed to be searched first. If your question is already answered then people downvote you.
The solution to your question:
In your mysql or phpmyadmin you can set whether you use a password or not. The best way to learn is to set mysql with a password in my opinion. If you will launch a website online finally, you have to take security measures anyway.
If you make contact to your mysql database with you have to set:
username, password, database and servername ( for instance localhost).
The most secure way is using the OOP / prepared method:
$servername ='localhost';
$username='yourname';
$password='12345';
$dbname='name_database';
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($stmt = $conn->prepare("SELECT idnum, col2, col FROM `your_table` WHERE idnum ='5' ")) {
$stmt->execute();
$res = $stmt->get_result();
$qrow = mysqli_num_rows($res);
while ($row = mysqli_fetch_assoc($res)) {
var_dump($qrows); // number of rows you have
$total = implode(" / " , $row);
var_dump($total);
$idnum = $row['idnum'];
var_dump($idnum);
}
The easiest way that I do with my site is make a file called db.php containing:
<?php
$host = 'localhost';
$user = 'root';
$pass = 'password';
$db = 'databasename';
$mysqli = new mysqli($host,$user,$pass,$db) or die($mysqli->error);
..then in the index.php file, at the top:
<?php
require_once('db.php')
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
Errors are Warning:
mysql_connect() [function.mysql-connect]: Access denied for user
'test'#'10.1.1.23' (using password: YES) in
/home/a4228948/public_html/imageid.php on line 9
Warning: mysql_select_db(): supplied argument is not a valid
MySQL-Link resource in /home/a4228948/public_html/imageid.php on line
10
Warning: mysql_close(): supplied argument is not a valid MySQL-Link
resource in /home/a4228948/public_html/imageid.php on line 29
<?PHP
error_reporting(E_ALL ^ E_DEPRECATED);
$user_name = "test";
$password = "protection";
$database = "temp";
$server = "mysql6.000webhost.com";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "SELECT * FROM test";
$result = mysql_query($SQL);
while ( $db_field = mysql_fetch_assoc($result) ) {
print $db_field['id'] ;
}
mysql_close($db_handle);
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}
?>
Error in Your Username or Password.
You check database Username and Password is correct . and server value set localhost
Only first error is really important at this point, the rest could be fine, but at the line 9 you won't connect, so nothing more be done with the DB.
Please double check that case is right etc. (depending on MySQL settings DB name can be case sensitive too), and that there are no extra spaces (happened to me a few times when copy-pasting).
Check that the DB actually exists (via phpMyAdmin or whatever your provider uses).
Check Database_name, Hostname and Login credentials (Username ans password) In term of case sensitive, spelling etc.
If you have correct credentials, database_name and Hostname then
check permission of user and grant permission.
Try
GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'#'%' WITH GRANT OPTION;
This is how I create my "Super User" privileges (although I would normally specify a host).
IMPORTANT NOTE
While this answer can solve the problem of access, WITH GRANT OPTION creates a MySQL user that can edit the permissions of other users.
The GRANT OPTION privilege enables you to give to other users or remove from other users those privileges that you yourself possess.
For security reasons, you should not use this type of user account for any process that the public will have access to (i.e. a website). It is recommended that you create a user with only database privileges for that kind of use.
grant all privileges on mydb.* to myuser#'%' identified by 'mypasswd';
grant all privileges on mydb.* to myuser#localhost identified by 'mypasswd';
Warning 2 -
Change
$db_found = mysql_select_db($database, $db_handle);
to
$db_found = mysql_select_db($database_spyware, $db_handle) || die(mysql_error());
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have these usernames and passwords:
User name: AmirAman
Pin: ****
Password: ***************1
Parallels Plesk: qatarreal
password: *************2
DataBase:
username: amir
password: ***3
When I type this code in index.php file, it doesn't work and Internal Server Error appears:
<?php
$username = "AmirAman";
$password = "**********1";
$hostname = "www.qatarreal-estate.";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
thank you for this answers , i tried all possible suggestion , but there are no thing changed , same result appear in this website qatarreal-estate.com.
DataBase:
username: amir
password: ***3
You will need to use these, like the following:
$username = "amir";
$password = "***3";
$hostname = "localhost";
I'm assuming the hostname of the mysql database is local, due to it not being defined in the information and webhosting providers sometimes leave that information out if it's on localhost.
Also note that you are using mysql_* functions which are considered bad practice and will be removed from PHP in the near future. Better is to use the PDO class or at least mysqli_* functions.
As other people pointed out in the comments, your hostname is wrong. Try this:
enter code here
<?php
$username = "AmirAman";
$password = "**********1";
$hostname = "qatarreal-estate.com";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
Assuming qatarreal-estate.comis the domain you are looking for.
Edit: If you run the MySQL server locally, use localhost or 127.0.0.1 for the hostname.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a PHP script which I use to connect to a MySQL database. Connection through mysql_connect works perfectly, but when trying with PDO I get the following error:
SQLSTATE[HY000] [2005] Unknown MySQL server host 'hostname' (3)
the code I use to connect is below:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$hostname_localhost ="hostname";
$database_localhost ="dbname";
$username_localhost ="user";
$password_localhost ="pass";
$user = $_GET['user'];
$pass = $_GET['pass'];
try{
$dbh = new PDO("mysql:host=$hostname_localhost;dbname=$database_localhost",$username_localhost,$password_localhost);
echo 'Connected to DB';
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("SELECT check_user_company(:user,:pass)");
$stmt = $dbh->bindParam(':user',$user,PDO::PARAM_STR, 16);
$stmt = $dbh->bindParam(':pass',$pass,PDO::PARAM_STR, 32);
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row)
{
echo $row['company_id'].'<br />';
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Thanks in advance
Got the same problem. Mine solution was another database port. I wrote localhost:1234 and got this error.
Fixed with:
mysql:host=$hostname_localhost;port=1234;dbname=$database_localhost",$username_localhost,$password_localhost);
echo 'Connected to DB';
It does seem pretty straightforward, here is what I use to build my PDO connectors(noticed your dbname and host are done differently than mine, dunno if that's relevant, but worth a check):
PDO Creation function
require_once('config.inc.php');
function buildDBConnector(){
$dsn = 'mysql:dbname='.C_BASE.';host='.C_HOST;
$dbh = new PDO($dsn, C_USER, C_PASS);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
}
config.inc.php
define('C_HOST','localhost');// MySQL host name (usually:localhost)
define('C_USER','sweetUsername');// MySQL username
define('C_PASS','sweetPassword');// MySQL password
define('C_BASE','superGreatDatabase');// MySQL database
And while it makes no sense, when I tried to declare $dsn inline including variables during the newPDO call, I kept getting failures too. I broke it apart and used the $dsn variable to do so. And haven't had an issue since.
Wondering if you're in shared hosting by chance?
NOTE:
If you don't have a dedicated IP, and instead are going through a NAT, your IP won't properly translate to your actual server.
That help at all?
UPDATE:
Just thought of another thing. Are you trying to connect to a mysql database that is on a different IP than you are running your scripts from? If so, you will likely need to enable remoteSQL access for the ip you are calling the database from. Fairly easy to do, but CRITICAL if you are not accessing localhost.
You dont seem to specify the database host dns details or IP address. Adding that will solve the problem