cannot connect to server using php - php

<?php
$host="localhost";
$user="user";
$password="debashree";
$connect=mysqli_connect($host,$user,$password);
if ($connect->connect_error) {
die("connection_aborted".$connect->connect_error);
}
echo "connected succesfully";
This is the mysql connection php script. It is constantly showing the error of
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'user'#'localhost' (using password: YES) in C:\xampp\htdocs\index.php on line 6
Notice: Trying to get property of non-object in C:\xampp\htdocs\index.php on line 7
connected successfully
I cannot understand please help me?

You should try the following changes made :
<?php
try{
$host="localhost";
$user="user";
$password="debashree";
$databaseName = "myDB";
$connect=mysqli_connect($host,$user,$password, $databaseName);
if (**mysqli_connect_errno()**)
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "connected succesfully";
}catch(Exception $e){
echo $e->getMessage();
}
Also, mysqli_connect requires database as fourth parameter.
mysqli_connect("localhost","my_user","my_password","my_db");

Related

Connecting PHP with MySQL DB in XAMPP issues

I'm trying to connect my PHP to my XAMPP DB server, but as it seems I'm not doing something right.
This is my code:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$db= "blogdata";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
And it returns:
Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'username'#'localhost' (using password: YES) in C:\xampp\htdocs\KungFu\Blog.php on line 8
Connection failed: Access denied for user 'username'#'localhost' (using password: YES)
I think everything is correct, tried different variations but as you can tell it is not working.
Best way to connect php to MySQL is to use new method : PDO :)
PHP :
$bdd = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', '');

PHP, MySQL returns "Connection failed: The server requested authentication method unknown to the client" [duplicate]

This question already has answers here:
PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client [duplicate]
(8 answers)
Closed 2 years ago.
Trying to connect to local Mysqli DB.
Database Connection:
<?php $con= new mysqli("localhost","Kobe24","Kobei987","Bkn_Data");
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
Returns this:
Warning: mysqli::__construct(): (HY000/2054): The server requested authentication method unknown to the client in C:\Apache24\htdocs\poc\practice_project\database_connection.php on line 1
Connection failed: The server requested authentication method unknown to the client
Any feedback would help. Been researching this problem but no definitive solutions.
Can you try this mysqli_connect() functions as per code below.
Please also ensure that database user and password are correct and that the user has permission to connect to the database
<?php
$dbhost = 'localhost:3306';
$dbuser = 'enter database user';
$dbpass = 'enter password';
$con = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $con ){
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($con);
?>

000webhost mysql connection error

I am trying to connect to my database using the following code:
<?php
$mysql_host = "mysql*.000webhost.com";
$mysql_database = "a******_account";
$mysql_user = "a******_admin";
$mysql_password = "******";
// Create connection
$con=mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_database);
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
};
?>
Does anyone know what is going on?
The error messages are:
Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'a******_admin'#'0.0.0.0' (using password: YES) in /home/a3996154/public_html/index.php on line 18
and
Failed to connect to MySQL: Access denied for user 'a******_admin'#'0.0.0.0' (using password: YES)
Probably: Your auth is wrong, or that user doesn't have permission to access to that database.
Sorry, it took a day for the database to respond, today it is up and running. Excuse me for any time I wasted.

Can't connect to database with 000webhost

I'm trying to learn php but when I attempt to connect to a mysql database, I get this error. I don't think anything is wrong with the code itself but perhaps there is an error on the hosts side? I'm using 000webhost so.
Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied
for user 'a7976620_db1user'#'localhost' (using password: YES) in
/home/a7976620/public_html/index.php on line 17
Here is my php:
<?php
$servername = "localhost";
$username = "a7976620_db1user";
$password = "******";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
In https://www.000webhost.com, there are two options in the cPanel:
Fix ownership and
Fix file permissions
You can apply this and it will be fine.

How to connect user name password protected ms access 2000 database (.*mdb) using php

Hi guys i want to connect USERNAME,PASSWORD protected MS ACCESS 2000 (.*mdb) database using PHP, ODBC or any other database connection. But given below code show WARNING and doesn't connect to the database. please anyone help.
CODE 1
$mdbFilename = "C:\Documents and Settings\Hp\Desktop\Door\DB\IMSDB.mdb";
$user = 'test';
$password ='123';
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};DBQ=$mdbFilename;",$user,$password);
if($connection)
{
echo "Success";
}
else
{
exit("Connection Failed: ".$connection);
}
WARNING MESSAGE
Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Microsoft Access Driver] You do not have the necessary permissions to use the '(unknown)' object. Have your system administrator or the person who created this object establish the appropriate permissions for you., SQL state 42000 in SQLConnect in C:\xampp\htdocs\punch\test.php on line 6
Output -> Connection Failed
CODE 2
I tried given below code also. there are no any ERRORS or WARNING MESSAGE but not connect to the access database.
$mdbFilename = "C:\Documents and Settings\Hp\Desktop\Door\DB\IMSDB.mdb";
$systemDbFilename = "C:\Documents and Settings\Hp\Desktop\Door\DB\SYSTEM.MDW";
$user = 'test';
$password ='123';
$adoCon = new COM("ADODB.Connection") or die("Cannot start ADO");
$connection= $adoCon->Open("Driver={Microsoft Access Driver (*.mdb)};DBQ=$mdbFilename; SystemDB=$systemDbFilename;Uid=$user; Pwd=$password;");
if(($connection->State) > 0)
{
echo "Success";
}
else
{
exit("Connection Failed: ".$connection);
}
Output -> Connection Failed
CORRECT ANSWER
Finally I found the answer. I tried PDO to connect ODBC.
WORKING CODE
try {
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=D:\Door\DB\IMSDB.mdb;SystemDB=D:\Door\DB\SYSTEM.MDW;Uid=test;Pwd=123;");
if($dbh)
{
echo "Connection Success";
}
}
catch (PDOException $e)
{
echo $e->getMessage();
}

Categories