Unable to connect PHP application with MySQL - php

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.

Related

Trouble connecting to a local database using PDO [duplicate]

This question already has answers here:
PHP PDO SQLite connection
(2 answers)
Closed 1 year ago.
I have created a database using DB browser for sqlite. I would like to connect to the database using a PDO function in php. My project currently has the php dataBaseHandler file and database file in the same folder. And then the index.php file which the database is going to be used in, is in a different folder (all under the same root folder). Basically right now when using the PDO to connect to the database i am getting an error saying Connection failed: SQLSTATE[HY000] [1049] Unknown database 'jobs.db'
Here is my PDO code
<?php
class Dbh {
private $servername;
private $username;
private $password;
private $dbname;
private $charset;
public function connect() {
$this->servername = 'localhost';
$this->usernamae = 'root';
$this->password = '';
$this->dbname = 'jobs.db';
$this->charset = 'utf8mb4';
try {
$dsn = "mysql:host=".$this->servername.";dbname=".$this->dbname.";charset=".$this->charset;
$pdo = new PDO($dsn, $this->usernamae, $this->password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
} catch (PDOException $e) {
echo "Connection failed: ".$e->getMessage();
}
$dsn = "mysql:host=".$this->servername.";dbname=".$this->dbname.";charset=".$this->charset;
$pdo = new PDO($dsn, $this->usernamae, $this->password);
return $pdo;
}
}
?>
And here is a photo of my folder structure, showing where the database file is relative to the DataBaseHandler file is.Website Structure
I should also mention that my code is being run through XAMPP.
You should use something similar to $dsn = "sqlite:" . $this->dbname; to connect to the SQLite database file.

How connect with mysql database in other server by php

my question is about connect with mysql database in other server by php ??
Server A is rec.misuratau.edu.ly, Server B is alrand.ly , I need to code pls.
I am not sure if I understand well. You are asking how to make a connection to mysql database on two different server?
You can do it like this:
First create databases on server, create user define password and grant access to the user.
Create two separate file: First is Config.php
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'http://rec.misuratau.edu.ly'); // replace with servername
define('DB_NAME', 'db');
define('USER_CREATED_SUCCESSFULLY', 0);
define('USER_CREATE_FAILED', 1);
define('USER_ALREADY_EXISTED', 2);
Second is DbConnect.php
class DbConnect {
private $conn;
function __construct() {
}
/**
* Establishing database connection
* #return database connection handler
*/
function connect() {
include_once dirname(__FILE__) . '/Config.php';
// Connecting to mysql database
$this->conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check for database connection error
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_set_charset($this->conn,"utf8");
// returing connection resource
return $this->conn;
}
}
LAter you just call the method before sql statements.
Please check the link below as well:
https://www.w3schools.com/php/php_mysql_connect.asp

Warning: mysqli::mysqli(): (28000/1045): Access denied [duplicate]

This question already has answers here:
MySQLI 28000/1045 Access denied for user 'root'#'localhost'
(2 answers)
Closed 6 years ago.
I noticed this has been asked several times, but I can't find an answer that works. I'm writing a class to handle my database calls, and for some reason can't establish a connection.
This is the error I received:
Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'root'#'localhost' (using password: YES) in...
This is the code that attempts to create an instance of the class:
<?php
require_once 'inventoryconfig.php';
require("MySQLiClass.inc");
ini_set('display_errors',1); error_reporting(E_ALL);
echo "control";
/* works */
$connection = new mysqli($db_hostname, $db_username, $db_password, $db_database, $db_port);
if ($connection->connect_error) die($connection->connect_error);
/* doesn't */
$mysqli_db = new mysqli_db($db_hostname, $db_username, $db_password, $db_database, $db_port);
?>
Here's the class:
<?php
require_once 'IRCinventoryconfig.php';
require("drawTable.inc");
ini_set('display_errors',1); error_reporting(E_ALL);
class mysqli_db
{
protected $db_hostname;
protected $db_database;
protected $db_username;
protected $db_password;
protected $db_port;
protected $connection;
function __construct($db_hostname, $db_database, $db_username, $db_password, $db_port)
{
$this->db_hostname = $db_hostname;
$this->db_database = $db_database;
$this->db_username = $db_username;
$this->db_password = $db_password;
$this->db_port = $db_port;
if (!empty($db_hostname)
|| !empty($db_database)
|| !empty($db_username)
|| !empty($db_password)
|| !empty($db_port))
{
$this->_connect();
} else {
die("You did not provide enough information, please check your configuration files and try again.");
}
}
public function _connect()
{
/* echoes the correct information */
echo $this->db_hostname;
echo $this->db_database;
echo $this->db_username;
echo $this->db_password;
echo $this->db_port;
$this->connection = new mysqli($this->db_hostname, $this->db_username, $this->db_password, $this->db_database, $this->db_port)
or die("Database access failed:" . $this->connection->error);
//$this->connection = new mysqli($this->db_hostname, $this->db_username, $this->db_password, $this->db_database, $this->db_port);
//if ($this->connection->connect_error) die($this->connection->connect_error);
}
}
Your arguments are being provided in the incorrect order to the constructor.
$mysqli_db = new mysqli_db($db_hostname, $db_username, $db_password, $db_database, $db_port);
function __construct($db_hostname, $db_database, $db_username, $db_password, $db_port)
The constructor should probably look like this:
function __construct($db_hostname, $db_username, $db_password, $db_database, $db_port)
First all the parameters need to hold a value so:
if (!empty($db_hostname)
|| !empty($db_database)
|| !empty($db_username)
|| !empty($db_password)
|| !empty($db_port))
{
sounds wrong. I'd suggest
if (!empty($db_hostname)
&& !empty($db_database)
&& !empty($db_username)
&& !empty($db_password)
&& !empty($db_port))
{
...and the order of the parameters you pass to the construct is wrong. It must follow the mysqli construct order (host,user,pass,database,port)
That happens if you have done any custom codes (usually).when you transfer the site then it contain info of previous wordpress setup.The info don't match with the site actual login data. So after transferring site do change the info.
in my case it is in function.php
It will look like this :
$server="localhost";
$dbuser="wrdp14";
$pwd="EIzAT3vu";
$db="wrdp14";
$this->conn=new mysqli($server,$dbuser,$pwd,$db);
if ($this->conn->connect_error)
just replace it with your recent wordpress data you can find that in your wp-config file

PDO no such file or directory - only ftp server access

I'm moving our php administration to a new server (hosted) but while trying out the migrated system, I detected that our code is unable to access the MySql database. The code was working before and the new webhost says it is supporting PDO. We have no ability to access the actual server more than via ftp and phpmyadmin, preventing us from accessing php.ini. We would prefer to be able to keep using PDO.
Any ideas how to fix this, and if not
How could we have this file working in the same/a similar way but without PDO?
Class Database{
protected $_link;
public function __construct(){
$config['db'] = array(
'hostname' => 'localhost', //The provided XXX.loopia.se and 127.0.0.1 has also been attemped here with no result
'dbname' => 'database_name',
'username' => 'user_name',
'password' => 'password'
);
$db = new PDO('mysql:hostname=' . $config['db']['hostname'] .';dbname=' . $config['db']['dbname'], $config['db']['username'], $config['db']['password']);
$this->_link = $db;
}
}
Log output:
PHP Warning: PDO::__construct() [pdo.--construct]: [2002] No such file or
directory (trying to connect via unix:///tmp/mysql.sock)
Link to loopia's "PHP to SQL"-use-this-code: https://support.loopia.se/files/php_till_mysql.zip
After quick feedback from the webhost, the above does not seem to be the preffered way to accomplish this at loopia. For further reference for other googlers, this is the code received (and edited to match the above purpose):
<?php
Class Database{
protected $_link;
public function __construct(){
$hostname = "xXXX.loopia.se"; //Log on to phpmyadmin and see what it says in the top after "server"
$username = "username";
$password = "password";
try {
$db = new PDO("mysql:host=$hostname;dbname=<DBNAME>",
$username,
$password,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$this->_link = $db;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
?>

Including php file with database connection error

I want a generic php file with all the database info (dbname,host,username,password)
But when I include the page, in like index.php I get this error:
Access denied for user 'apache'#'localhost' (using password: NO)
connect.php
<?php
class dbconnect{
private $host = '**';
private $user = '**';
private $pass = '**';
public $con;
function Connect($db = '**') {
if($db=='**'){
$this->host="localhost";
$this->user="**";
$this->pass="**";
$db="**";
}else{
$this->host="**";
$this->user="**";
$this->pass="**";
}
$this->con = mysql_connect($this->host,$this->user,$this->pass);
if (!$this->con)
{
die('Could not connect: ' . mysql_error());
}
$blaa = mysql_select_db($db, $this->con);
mysql_query("SET NAMES UTF8");
return $blaa;
}
function Disconnect() {
//$this->con = mysql_connect($this->host,$this->user,$this->pass);
mysql_close();
}
}
?>
I am sure the ** information is correct because when I specify it as:
$con=mysqli_connect("example.com","example","password","my_db");
In index.php it works
It's important to note, your test case doesn't actually prove it works.
What does this output:
$conn = mysql_connect("example.com", "user", "password");
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
As you won't necessarily get the information that's failing it without that.
To top that off, let's simplify your class a little bit for debugging purposes:
class dbconnect
{
private $host = '**';
private $user = '**';
private $pass = '**';
public $con;
public function Connect($host = "localhost", $user = "root", $pass = "")
{
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->con = mysql_connect($this->host, $this->user, $this->pass);
if (!$this->con) {
die('Could not connect: ' . mysql_error());
}
$blaa = mysql_select_db($db, $this->con);
mysql_query("SET NAMES UTF8");
return $blaa;
}
public function Disconnect()
{
mysql_close($this->con);
}
}
Now what do you get when you do
$db = new dbconnect("example.com", "user", "password");
Be sure you're using credentials that work, and that you're not running into issues such as default values or incorrect variable assignment through these methods.
Now, if you don't want to provide the values, you can simply:
$db = new dbconnect();
Public Service Announcement
Check out PHP's PDO or at minimum (but really, just use PDO) the mysqli alternative. PHP's mysql extension is NOT secure, and you should not be using it in any environment, ever.
If the connection information are correct, check your MySQL User's host access permission:
SELECT user, host FROM mysql.user
If "localhost" is set, then the user can only access the database locally, otherwise "%" will open access.
Usually it is an issue with the connection credentials.
Check that you can log into your mysql using the details you have set for that website.
mysql -u apache -p
It will then ask you for the password.
If that login does not work, then you have a problem with that user's mysql account.
You use incorrect parameters for accessing. Just dump variables which at the line $this->con = mysql_connect($this->host,$this->user,$this->pass);. You can use debugger or echo, print instructions.
Furthermore use PDO extension for accessing to databases. It's better!
Why shouldn't I use mysql_* functions in PHP?

Categories