My friend and I are working on a website, and I am trying to install a build of it on my machine. It is already running live on the server, I just can't seem to connect from my local machine. When I do I get the following error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045]
Access denied for user 'myusername'#'adsl-client.example.net' (using password: YES)' in /Users/myname/Sites/mysitefolder/req/connect.php:25
Stack trace: #0 /Users/myname/Sites/mysitefolder/req/connect.php(25): PDO->__construct('mysql:host=mysq...', 'myusername', 'mypassword')
#1 /Users/myname/Sites/burn-after-reading/inc/header.php(9): ConnectionFactory->getConnection()
#2 /Users/myname/Sites/burn-after-reading/index.php(79): include('/Users/myna...')
#3 {main} thrown in /Users/myname/Sites/mysitefolder/req/connect.php on line 25
Here is the file I am trying to connect from, connect.php
<?php
class ConnectionFactory
{
var $dbhost = 'mysql.mysitename.io';
var $dbname = 'my_database_name';
var $dbusername = 'myusername';
var $dbpassword = 'mypassword';
private static $factory;
public static function getFactory()
{
if (!self::$factory)
self::$factory = new ConnectionFactory();
return self::$factory;
}
public function getConnection()
{
$conn = new PDO("mysql:host={$this->dbhost};dbname={$this->dbname}",
$this->dbusername,
$this->dbpassword);
return $conn;
}
public function getConnectionNoName()
{
$connNoName = new PDO(
"mysql:host={$this->dbhost}",
$this->dbusername,
$this->dbpassword
);
return $connNoName;
}
}
For security reason you need to add your IP address to the list of allowed remote mysql connections.
Related
here i am trying to connect to MySQL database using PHP-PDO from remote server using IP address. when put ip address in place of host it gives me following error
Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in D:\xampp\htdocs\oppInsights\database\Database.php on line 32
Fatal error: Uncaught exception 'Exception' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. ' in D:\xampp\htdocs\oppInsights\database\Database.php:39 Stack trace: #0 D:\xampp\htdocs\oppInsights\database\Select.php(800): Database->Connection() #1 D:\xampp\htdocs\oppInsights\decision.php(19): Select->expiryContracts() #2 {main} thrown in D:\xampp\htdocs\oppInsights\database\Database.php on line 39
this is the code
<?php
class Database {
public $dbhost = "mysql:dbname=apt;host=http://10.75.225.171:3601";
public $dbuser = "tribhuvan";
public $dbpass = "123456";
public $dbname = "apt";
public $connection;
public $selectdb;
public $isConnected;
public $dbh;
//$user = 'dbuser';
//$password = 'dbpass';
public function Connection()
{
try
{
$this->dbh = new PDO($this->dbhost, $this->dbuser, $this->dbpass);
// echo "true";
return $this->dbh;
}
catch(Exception $e)
{
$this->isConnected = false;
throw new Exception($e->getMessage());
}
}
public function Disconnect()
{
$this->datab = null;
$this->isConnected = false;
}
}
?>
i have checked username and password they seems to be same as i gave.thank you in advance.
You need to remove http from the host and put the port number under port attribute.
Please try with this line :
$dbhost = "mysql:host=10.75.225.171;port=3601;dbname=apt";
From a friend, I got a website (zip file) with PHP and HTML files in it. I also got a SQL script from him. Now what I'm trying to do is run the application on localhost (Xampp apache server) and the website does load, but it looks like I can't connect to the database.(I did import the SQL script and it worked, But I'm still getting this error) This is an error for example that I'm receiving on the home page:
Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'dewaai'#'localhost' (using password: YES)
Notice: Undefined index: logged in C:\xampp\htdocs\deWaai\includes\header.php on line 14
The name of the database is 'dewaai'
The username is: root
And I think there is no password...
This is the connection.php file:
<?php
session_start();
include "db.php";
$db = new db();
$db->setDB("localhost", "dewaai", "dewaai", "dewaai");
$db->connect();
?>
These are database functions in the db.php file:
<?php
class db {
private $conn;
private $servername;
private $dbuser;
private $dbpass;
private $dbname;
function setDB($servername, $dbuser, $dbpass, $dbname) {
$this->servername = $servername;
$this->dbuser = $dbuser;
$this->dbpass = $dbpass;
$this->dbname = $dbname;
}
function connect() {
try {
$this->conn = new PDO("mysql:host=$this->servername;dbname=$this->dbname", $this->dbuser, $this->dbpass);
// set the PDO error mode to exception
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
}
Does anyone know how I can fix this so the database runs the application?
I really don't know why I'm getting that error.
Any kind of help is appreciated, thx
There is problem with your username or password.
$db->setDB("localhost", "root", "", "dewaai");
Try this it will help you to connect with your database
Hi the issue clearly from the error message states that your username and password given in connection.php file is incorrect.
The format for DB connection is
$db->setDB( Database host IP address or DNS name , Username , Password, DatabaseName);
From what you said i guess connection.php file should be
<?php
session_start();
include "db.php";
$db = new db();
$db->setDB("localhost", "root", "", "dewaai");
$db->connect();
?>
And i think your PDO connection might have an issue as well. It should be
$this->conn = new PDO('mysql:host='.$this->servername.'; dbname='.$this->dbname, $this->dbuser, $this->dbpass);
Hope this helps.
I have set up for the first time the authentication for MongoDB. I have two users: 'admin', (set as root in the 'admin' database) and 'testUser' which is set up as 'dbAdmin' in the 'testDatabase'.
When I use mongo shell to login using the following command everything works:
mongo -u testUser -p abcd1234 --authenticationDatabase testDatabase
On the PHP's end, I have the following code:
<?php
class DBConnection {
const HOST = '1.1.1.1';
const PORT = 27017;
const DBNAME = 'testDatabase';
const USERNAME = 'testUser';
const PASSWORD = 'abcd1234';
private static $instance;
public $connection;
public $database;
private function __construct() {
if (!extension_loaded('mongo')) die("MongoDB is not installed!");
try {
$this->connection = new MongoClient('mongodb://'.self::HOST.':'.self::PORT.'/'.self::DBNAME, array('username' => self::USERNAME, 'password' => self::PASSWORD));
$this->database = $this->connection->selectDB(self::DBNAME);
} catch (MongoConnectionException $e) {
throw $e;
}
}
static public function instantiate() {
if (!isset(self::$instance)) {
$class = __CLASS__;
self:: $instance = new $class;
}
return self::$instance;
}
public function getCollection($name) {
return $this->database->selectCollection($name);
}
public function execute($code) {
return $this->database->execute($code);
}
}
?>
Naturally, host (as well as the db name, username and password) are obfuscated. I have verified multiple time that there is not a typo in the credentials. I have also verified that I can connect to the database from a remote shell, similar to how this script connects.
Still, I always get this error:
PHP Fatal error: Uncaught exception 'MongoConnectionException' with
message 'Failed to connect to: 1.1.1.1:27017: Authentication
failed on database 'testDatabase' with username 'testUser': auth failed' in
/var/www/html/wip/include/mongoConnect.php:17 Stack trace:
0 /var/www/html/wip/include/mongoConnect.php(17): MongoClient->__construct('mongodb://1.1...', Array)
1 /var/www/html/wip/include/mongoConnect.php(27): DBConnection->__construct()
2 /var/www/html/wip/migration/migrate.php(85): DBConnection::instantiate()
3 {main} thrown in /var/www/html/wip/include/mongoConnect.php on line 17
Credentials for both users are in SCRAM-SHA-1.
Any idea what is causing this connection issue?
You can try to change the way of logging in
$m = new MongoClient("mongodb://${username}:${password}#localhost");
Take a look at here:
http://php.net/manual/en/mongo.connecting.auth.php
I am trying to catch an error if I cannot connect to database (eg xamp down or database connection down etc). I have tried to use PDO::errorCode() but yield no reuslts.
In my php I have a connection.php and a method ' to connect to the datatbase many times and return a new PDO instance to achieve various queries. The response displays the error message then all the php queries that call it, which also includes the name and password of the database in a non encrypted format.
How can I catch this error and replace this error message with a human readable alternative (that doesnt display the name and password)?
CONNECTION.PHP
function connect()
{
// set database server access variables:
$host = "localhost";
$user = "root";
$pass = "password";
$dbase = "dbName";
//Establish a connection
$db = new PDO("mysql:host=".$host."; dbname=".$dbase."", $user, $pass); //line 16
$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
return $db;
}
RESPONSE
Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[HY000] [2002] No connection could be made because the target
machine actively refused it. ' in
D:\Users\Username\Dropbox\Web\htdocs\sitename\php\data\connection.php:16
Stack trace: #0
D:\Users\Username\Dropbox\Web\htdocs\sitename\php\data\connection.php(16):
PDO->__construct('mysql:host=loca...', 'root', 'password') #1
D:\Users\Username\Dropbox\Web\htdocs\sitename\php\function\active-user.php(28):
connect() #2
D:\Users\Username\Dropbox\Web\htdocs\sitename\map-floorplan.php(10):
include('D:\Users\Username...') #3 {main} thrown in
D:\Users\Username\Dropbox\Web\htdocs\sitename\php\data\connection.php
on line 16
do a try/catch to catch exceptions
try{
$db = new PDO("mysql:host=".$host."; dbname=".$dbase."", $user, $pass); //line 16
$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
return $db;
} catch( PDOException $e){
$originalError = $e->getMessage();
echo 'something went wrong.. '.$originalError;
exit;
}
Is the connect() function depecrated or something? When I try to connect to a mysql database using that function, my page stops working?
i'm doing jeffrey ways 30daysjquery course lesson 25
here's the code in my index.php file
<?php
require 'functions.php'
if ( isset($_POST['q'])) {
connect();
}
include 'views/index.tmpl.php'
?>
This is my code in my functions.php file
<?php
function connect(){
global $pdo; // set it as global so other functions can access it
$pdo = new PDO("mysql:host=localhost;dbname=sakila", "root", "root");
}
function get_actors (){
}
?>
Might it be that I do not have access to the content that's in functions.php? if it is how do I create access?
Your function should maybe return a connection:
function connect(){
$pdo = new PDO("mysql:host=localhost;dbname=sakila", "root", "root");
return $pdo;
}
Then, your function get_actors() could be
$db_connection = connect();
get_actors( $db_connection );
function get_actors( $pdo ) {
//
// You use the database connection $pdo to get actors
//
}
You have to add a parameter to your function get_actors. No need to modify the code into the function.
When I checked MAMP this is the information I have if I want to connect to a MySQL server using a script.
Host localhost
Port 8889
User root
Password root
Socket /Applications/MAMP/tmp/mysql/mysql.sock
thrown in /Applications/MAMP/htdocs/lesson-25/functions.php on line 5
[09-Nov-2014 23:01:47 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in /Applications/MAMP/htdocs/lesson-25/functions.php:5
Stack trace:
#0 /Applications/MAMP/htdocs/lesson-25/functions.php(5): PDO->__construct('mysql:host=loca...', 'root', 'root')
#1 /Applications/MAMP/htdocs/lesson-25/functions.php(9): connect()
#2 /Applications/MAMP/htdocs/lesson-25/index.php(3): require('/Applications/M...')
#3 {main}