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;
}
}
}
Related
The error is:
Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'moomen'#'localhost' (using password: YES)
Code is:
<?php
$servername = "localhost";
$username = "moomen";
$password = "9124279123";
$dbname = "ecocaa";
try {
$conn = new PDO("mysql:host=$servername;dbname=$servername", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
I can see a problem in your code
In the dbname you have used server name, change the code it will work.
I have setup an instance and now I want to connect my website with database. What would be the hostname If I want to connect to phpmyadmin. Will hostname be localhost?.I am using a following code:-
class Database{
// specify your own database credentials
private $host = "localhost";
private $db_name = "PHPLearning";
private $username = "root";
private $password = "";
public $conn;
// get the database connection
public function getConnection(){
$this->conn = null;
try{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
$this->conn->exec("set names utf8");
}catch(PDOException $exception){
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
your code is correct. but it not work then change host from localhost to 127.0.0.1
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);
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.
I am new to PHP and need to modify some code in order to compile with my Microsoft SQL Server. The original code is like this. I downloaded it from usercake
<?php
/*
UserCake Version: 2.0.2
http://usercake.com
*/
//Database Information
$db_host = "localhost"; //Host address (most likely localhost)
$db_name = "202"; //Name of Database
$db_user = "202"; //Name of database user
$db_pass = "password"; //Password for database user
$db_table_prefix = "uc_";
GLOBAL $errors;
GLOBAL $successes;
$errors = array();
$successes = array();
/* Create a new mysqli object with database connection parameters */
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
GLOBAL $mysqli;
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
//Direct to install directory, if it exists
if(is_dir("install/"))
{
header("Location: install/");
die();
}
?>
I already installed sqlsrv and tested the link. It works with my database. Then I changed the code to this:
<?php
//Database Information
$server = "servername";
$connectionInfo = array("Database"=>"databasename","UID"=>"xxxxxx", "PWD"=>"xxxxxx" );
$db_table_prefix = "uc_";
GLOBAL $errors;
GLOBAL $successes;
$errors = array();
$successes = array();
/* Create a new sqlsrv object with database connection parameters */
$mssqlsrv = new sqlsrv($server, $connectionInfo);
GLOBAL $mssqlsrv;
if(sqlsrv_connect_errno()) {
echo "Connection Failed: " . sqlsrv_connect_errno();
exit();
}
//Direct to install directory, if it exists
if(is_dir("install/"))
{
header("Location: install/");
die();
}
?>
I get the following error message:
Fatal error: Class 'mssql' not found in
I think this line is the problem:
$mssqlsrv = new sqlsrv($server, $connectionInfo);
But I do not know how to fix this.
I would use PDO in this case: http://www.php.net/manual/en/pdo.construct.php
You can create a DSN connection to SQL Server
$dsn = "sqlsrv:Server=servername;Database=databasename"
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
The information to connect to SQL server is available here: http://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php