Config file:
$db=array(
'host' => 'localhost',
'user' => 'root',
'pass' => 'secret',
'name' => 'jack',
);
Index file
$mysql = new mysqli($db['host'],$db['user'],$db['pass'],$db['name']);
$mysql->set_charset('utf8mb4');
if($mysql->connect_errno)
{
die('Database connection could not be established. Error number: '.$mysql->connect_errno);
}
I get:
Database connection could not be established. Error number: 1045
Please guys, tell me what to do and I will respond you with the answer in 30 seconds!
UPDATE:
Okay so if i make change line to
$mysql = new mysqli(localhost,root,secret,jack);
it works, but how do I get it work with the tags?
Error No: 1045 refer to as Access denied for user 'root'#'localhost' (Using password: NO). so check you password and username for the database. Try passing the password as empty.
Related
I am trying to connect to MySQL database with pdo but keep getting this error:
Warning: Use of undefined constant username - assumed 'username' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\first\index.php on line 2
Warning: Use of undefined constant password - assumed 'password' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\first\index.php on line 2
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'username'#'localhost' (using password: YES) in C:\xampp\htdocs\first\index.php:2 Stack trace: #0 C:\xampp\htdocs\first\index.php(2): PDO->__construct('mysql:host=loca...', 'username', 'password') #1 {main} thrown in C:\xampp\htdocs\first\index.php on line 2
here's my code:
<?php
$pdo = new PDO('mysql:host=localhost;port=3306;dbname=dbname', username, password);
?>
It's because the username and password are not variables. They are missing the $
<?php
$username = 'myUsername';
$password = 'myPassword';
$pdo = new PDO('mysql:host=localhost;port=3306;dbname=dbname', $username, $password);
?>
I use Xampp as server. I have mysql table that i visualize with bootstrap grid. As the table have more than 1000 rows, the visualization with bootstrap is slow. I'm trying to work server side but have a problem connecting to the data base with this code
$dbDetails = array(
'host' => 'localhost',
'user' => 'root',
'pass' => '',
'db' => 'company');
It can't contact the server or connect to the database, while with this is possible. I receive the following errors: DataTables warning: table id=memListTable - Invalid JSON response AND {"error":"An error occurred while connecting to the database. The error reported by the server was: SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO)"}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "company";
Warning: mysqli_connect(): (HY000/1045): Access denied for user
'usr'#'localhost' (using password: YES) in
/storage/emulated/0/htdocs/includes/condb.php on line 9 Failed to
connect to MySql: Access denied for user 'usr'#'localhost' (using
password: YES) Warning: mysqli_query() expects parameter 1 to be
mysqli, boolean given in /storage/emulated/0/htdocs/includes/condb.php
on line 19
Warning: mysqli_error() expects parameter 1 to be mysqli, boolean
given in /storage/emulated/0/htdocs/includes/condb.php on line 20
Unable to connect
Im trying to connect to the database using this code but im an unable to. I dont know what i actually did wrong. Ive tried googling and reading some articles but none of those work.
UPDATED CODE still getting the error message above.
$server = "127.0.0.1"; $username = "usr"; $password = "123"; $database
= "contents";
$conn = mysqli_connect($server,$username,$password,$database);
if(mysqli_connect_errno($conn)) {
echo "Failed to connect to MySql: ".mysqli_connect_error(); }
$sql = "INSERT INTO contents('subj','article','day') VALUES
('".$_POST['This is the subj']."', '".$_POST['This is the
content']."', '".$_POST['2019-02-01 10:15:59']."')";
if(!mysqli_query($conn,$sql)) {
die("Unable to connect".mysqli_error($conn)); }else {
echo "Connected"; }
?>
Still getting the same error above unfortunately it was not a typo error. Still couldn't get the solution to this problem it has been days. Can someone with problem solving skills help me.
You don't have to pass $conn inside mysqli_connect_errno()
remove $conn
$conn = mysqli_connect($server,$username,$password,$database);
if(mysqli_connect_errno()) {
echo "Failed to connect to MySql: ".mysqli_connect_error();
}
You can fix this error by changing your mysql username and password. There are many tutorials for changing mysql user and password with a little search
I have a WAMP installation with Windows Server 2012 R2 Datacenter Edition, Apache 2.4.23, PHP 7.0.10 (mysqlnd 5.0.12-dev - 20150407), MySQL 5.7.14.
I configured a connection with:
$host = 'wrong-server';
$dbase = 'db_name';
$user = 'my_user';
$pwd = 'my_pwd';
$connection='mysql: host='.$host.'; dbname='.$dbase;
$link = new PDO($connection , $user, $pwd);
where wrong-server doesn't exsist (it is for a test for catching errors).
When I execute the code the connection works using localhost as host (I know it because a query to db_name retrive datas).
I searched for some default behaviour or configuration without succes.
Some ideas?
Code update:
<?php
$host = '192.168.123.123'; //'localhost';
$dbase = 'db_name';
$user = 'my_user';
$pwd = 'my_pwd';
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
$connection='mysql: host='.$host.'; dbname='.$dbase;
try {
$link = new PDO($connection, $user, $pwd, $pdo_options);
$_SESSION['DB_INFO']=array( 'db'=>$link->getAttribute(PDO::ATTR_DRIVER_NAME),
'ver'=>$link->getAttribute(PDO::ATTR_SERVER_VERSION),
'cli'=>$link->getAttribute(PDO::ATTR_CLIENT_VERSION ),
'srv'=>$link->getAttribute(PDO::ATTR_SERVER_INFO ),
);
print_r($_SESSION['DB_INFO']);
}
catch(\PDOException $err) {
echo 'Error: '.$err->getMessage();
};
Server 192.168.123.123 doesn't exsist but I have these response:
Array ( [db] => mysql [ver] => 5.7.14 [cli] => mysqlnd 5.0.12-dev - 20150407 - $Id: 241ae00989d1995ffcbbf63d579943635faf9972 $ [srv] => Uptime: 20111 Threads: 1 Questions: 354 Slow queries: 0 Opens: 125 Flush tables: 1 Open tables: 118 Queries per second avg: 0.017 )
But if I set $pwd='wrong_pwd' the answer is:
Error: SQLSTATE[HY000] [1045] Access denied for user 'my_user'#'localhost' (using password: YES)
SOLUTION
As #MichaelBerkowski states, eliminating spaces in the DSN
mysql:host='.$host.';dbname='.$dbase.'
instead of
mysql: host='.$host.'; dbname='.$dbase.'
solves the problem, so an error came out.
I'm using Apache server on Windows 8, XAMPP 1.8.1, PHP 5.4.7 and MySQL.
I've decided to convert my code to PDO.
Checking my phpinfo() it appears that the PDO driver for MySQL is enabled, but I still keep on getting the "could not find driver" error.
Here's the code:
<?php
//connect to database
$config['db'] = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'x'
);
$db = new PDO("
pdo_mysql:host= " .$config['db']['host']. ";
dbname= " .$config['db']['dbname']. ";
username= " .$config['db']['username'].";
password= " .$config['db']['password']."
");
?>
And the error:
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver'
Any suggestions?
Let me suggest you refer to the manual page and get the proper DSN string example from there
What happens if you change the DSN prefix from pdo_mysql to just mysql?
As manual says link
You should format your dsn in this way:
DRIVER:host=YOURHOST;dbname=YOURDB
Also to create the instance you should act like this: $db = new PDO($dsn, $username, $password)
Also I forgot to say (as other users have pointed out) your DRIVER is "mysql" and not pdo_mysql
In the end there were 2 problems with the code:
1. pdo_mysql -> mysql
2. username and password should be passed as variables and not as strings.
The correct code is:
$config['db'] = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'x'
);
$db = new PDO (
"mysql:host=".$config['db']['host'].";dbname=".$config['db']['dbname']."",
$config['db']['username'],
$config['db']['password']
);