I'm running a mysql database (managed through phpmyadmin) on a lubuntu virtual machine, which itself is on a mac 10.6.
Everything was working smoothly until earlier, (and I didn't touch any part of the login code!) when I got the following error on my pages:
Erreur : SQLSTATE[28000] [1045] Access denied for user 'www-data'#'localhost' (using password: NO)
I don't understand where this error is coming from as I don't have a user called www-data on phpmyadmin and my files are supposed to be using the root account on the database:
$dbname = 'name';
$dbuser = 'root';
$dbpass = '*****';
$dbhost = 'localhost';
try{
$linkpdo = new PDO('mysql:host='.$dbhost.';'.$dbname.', '.$dbuser.' , '.$dbpass);
$linkpdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (Exception $e) {
die('Erreur : '.$e->getMessage());
}
After some googling I saw that people modified their config.conf file, but I don't know how or why.
Use a double quoted string and the correct parameters for the connect, its easier to build using a double quoted string and $variable expansion.
$linkpdo = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$linkpdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
The user and password are separate parameters, http://php.net/manual/en/pdo.connections.php.
So take out the concatanation here-> .', '.$dbuser.' , '.$dbpass.
Connection should be:
$linkpdo = new PDO('mysql:host='.$dbhost.';dbname='. $dbname, $dbuser, $dbpass);
<?php
$dsn = 'mysql:dbname=name;host=localhost';
$user = 'root';
$password = '*****';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
Try this code. Checkout this link for more details http://php.net/manual/en/pdo.connections.php
Try this :
$linkpdo = new PDO('mysql:host='.$dbhost.';dbname='.$dbname , $dbuser, $dbpass);
Related
I am relatively new to php and have been following a tutorial for a beginner project. This project includes a database using the DBO object with mysql. I am using MAMP to run php. When I try to run my project it gives me the error in the title. Here is my code for database setup:
<?php
$dsn = 'mysql:host=localhost;dbname=assignment_tracker';
$username = 'root';
try {
$db = new PDO($dsn, $username);
} catch (PDOException $e) {
$error = "Database Error: ";
$error .= $e->getMessage();
include('view/error.php');
exit();
}
I am able to enter the phpmyadmin page. Looking at the user table in the mysql database shows this:
My port in the php.ini file is 8889. (My apache port is 8888 but my mysql is 8889 not sure which one it should be set to.) Looking at other answers on this website have been really confusing for me so any help would be greatly appreciated.
Try adding mysql's port to the $dns and add the password (if it's not null) as part of the connection. Try this
<?php
$dsn = 'mysql:host=127.0.0.1;port=8889;dbname=assignment_tracker';
$username = 'root';
$password = '';
try{
$db = new PDO($dsn, $username, $password);
}catch (PDOException $e){
$error = "Database Error: ";
$error .= $e->getMessage();
include('view/error.php');
exit();
}
I am using PDO to connect to MySQL database. The code works perfect when run in local host. However when I transfer the code to web server (of course I have changed the user name and password accordingly) the code does not work. It gives the following error
SQLSTATE[HY000] [1045] Access denied for user 'xxx_xxxxx'#'localhost' (using password: YES)
When I test connection using old deprecated API - mysql_connect() with same user name password combination, it works perfect, so I know that the user name password combination is right.
Here is my relevant code
try {
$dsn = "mysql:dbname=company_dbname; host=localhost";
$user = "company_dbuser";
$password = "MyPassword";
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC);
$conn = new PDO($dsn, $user, $password, $options);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
Ultimately I am able to solve the problem
Here is my edited code
try {
$dsn = "mysql:dbname=company_dbname; host=localhost";
$user = 'company_dbuser';
$password = 'MyPassword';
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC);
$conn = new PDO($dsn, $user, $password, $options);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
Please check $user and $password assignment - instead of double quote I assigned single quoted value.
I'm having this problem where it says access denied, even though the login data is correct. I've tried multiple servers but it will keep saying access denied. The machine does have access to the server. I think it's my wamp that's not working.
Error:
Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'xml'#'localhost' (using password: YES)
Class:
class db
{
private $conn;
function __construct()
{
}
public function setupConnection()
{
if (!isset($conn)) {
$mysql_host = 'localhost';
$mysql_username = 'xml';
$mysql_password = '12345';
$mysql_db = 'xml';
try {
$conn = new PDO("mysql:host=" . $mysql_host . ";dbname=" . $mysql_db . "", $mysql_username, $mysql_password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
echo "Connectie is gefaald!: " . $e->getMessage();
}
return $conn;
}
}
}
I´m trying to connect to a SQL Server 2008 R2 via PHP. I used the following code:
try {
$server = "servername.do.ma.in\\MSSQLSERVER";
$user = "username";
$pass = "password";
$db = "databasename";
$dbh= new PDO('sqlsrv:Server = ' . $server . '; Database = ' . $db, $user, $pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Verbindung fehlgeschlagen: ' . $e->getMessage();
}
But when i run this code i get the following error:
SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87].
Does someone know this error and know what to do?
Thanks in advance.
Solution:
The Port was not defined. The following code worked for me.
I´ve added the Port 1433 to the $server Variable.
$server = "servername.do.ma.in, 1433";
$user = "username";
$pass = "password";
$db = "databasename";
$dbh = new PDO("sqlsrv:Server={$server};Database={$db};", $user, $pass);
This will work:
$dbh = new PDO("sqlsrv:Server={$server};Database={$db};", $user, $pass);
If it's still not valid, there's something wrong with your connection variables.
EDIT:
This looks similar to your problem:
http://laravel.io/forum/01-20-2015-sql-server-2012-db-connection-with-laravel-42
couldn't access database with error message:
SQLSTATE[28000] [1045] Access denied for user 'root’#‘xxx.ne.jp'(using password: YES)
I have this php code.
$dsn = 'mysql:dbname=mydb;host=xxx.ne.jp';
$user = 'root';
$password ='0123';
try{
$dbh = new PDO($dsn, $db_user, $db_password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo $e->getMessage();
}
I can login to phpmyadmin with same username and password(root/0123).
why?
Do you have any idea to fix it?
Here is the basic PDO connection example:
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
So include the missing host name:
$dsn = 'mysql:host=localhost;dbname=mydb;host=xxx.ne.jp';
$user = 'root';
$password ='0123';
try{
$dbh = new PDO($dsn, $db_user, $db_password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo $e->getMessage();
}
Change localhost if running site on another host.