PHP Error "mysqli::mysqli(): (HY000/2002)" [duplicate] - php

This question already has answers here:
mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket 'MySQL' (2)
(5 answers)
Closed 3 years ago.
I'm having a problem running php code. The error only appears for this php file, other simple ones work fine. I'm using xampp. apache and mysql running green.
<?php
$username = filter_input(INPUT_POST, 'username');
$password = filter_input(INPUT_POST, 'password');
if (!empty($username)){
if (!empty($password)){
$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "test";
// Create connection
$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
if (mysqli_connect_error()){
die('Connect Error ('. mysqli_connect_errno() .') '. mysqli_connect_error());
}
else{
$sql = "INSERT INTO account (username, password)
values ('$username','$password')";
if ($conn->query($sql)){
echo "New record is inserted sucessfully";
}
else{
echo "Error: ". $sql ." ". $conn->error;
}
$conn->close();
}
}
else{
echo "Password should not be empty";
die();
}
}
else{
echo "Username should not be empty";
die();
}
?>

Finally got the solution to this problem, I'm sharing it because I can't find a solution anywhere else. The $host should be linked to "127.0.0.1" but for an unknown reason my host only worked when it was "127.0.0.1:3307" or the name of your port for mysql at the end, don't forget the ":". PS I changed my db connection back to using the procedural way despite what the Ashu said and it worked.

When you are using Object-oriented mysqli connection then you have to use all Object-oriented method.
In below code you used procedural way:
if (mysqli_connect_error)
{
die('Connect Error ('. mysqli_connect_errno.') '. mysqli_connect_error);
}
Replace with :
if ($conn->connect_error)
{
die('Connect Error ('. $conn->connect_errno.') '. $conn->connect_error);
}
Use 127.0.0.1 instead of localhost

Related

Connection failed: No such file or directory (IONOS) [duplicate]

I am working on a payroll system. Everything is working fine on localhost (xampp) but when I upload it to ionos hosting, it is loading the admin login page fine but when i enter the username & password it is giving "Connection failed: No such file or directory".
DB name: dbs4868780
and here is the code:
<?php
$conn = new mysqli('localhost', 'root', '', 'xxxxxxxxx');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
EDIT:
I tried it but it is still giving the same error.
I also tried to connect DB with PHP code provided by ionos but it is still giving "Connection failed. File or Directory not found"
Here is the php code from ionos:
<?php
$host_name = 'db5005797255.hosting-data.io';
$database = 'dbs4868780';
$user_name = 'xxxxxxxxx';
$password = 'xxxxxxxxxxxxxxxxxxxx';
$link = new mysqli($host_name, $user_name, $password, $database);
if ($link->connect_error) {
die('<p>Failed to connect to MySQL: '. $link->connect_error .'</p>');
} else {
echo '<p>Connection to MySQL server successfully established.</p>';
}
?>
See attached fake database example to get your data

PHP file wont connect to the sql database, it says error #1045 but the values on the code are right [duplicate]

This question already has answers here:
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES) (SQL: select * from `table`)
(2 answers)
Closed 2 years ago.
I tried connecting my PHP file to MySQli database but when i ran the code it displays
But when i logon to phpmyadmin it works fine no errors i can login to my account with no problems
Credentials in the database:
Connection Code:
<?php
$con = mysqli_connect('localhost','sotomango', '1234', 'mysql');
if(!$con) {
echo 'noooo';
}
?>
I double checked the database usernames, passwords, and privileges. Everything seems in place and correct, but the php file wont allow me to connect to the database.
#Dharman had a nice post but I couldnt find it.
Try this for debug :
$con = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$con) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is
great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($con) . PHP_EOL;
mysqli_close($con);
mysqli_connect
if its a new instalation or new upgrade, You need to include your port into connection, mariadb comes as default port so, when you try to connect mysql its redirecting to mariadb.
you need to include your correct port into connection it might be 3306, 3307 or 3308, and use ip instead of localhost.
your final codes should look like this :
$host = '127.0.0.1';
$db = 'test';
$user = 'root';
$pass = '';
$port = '3308';
$charset = 'utf8mb4';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$mysqli = mysqli_connect($host, $user, $pass, $port, $db);
mysqli_set_charset($mysqli, $charset);
} catch (\mysqli_sql_exception $e) {
throw new \mysqli_sql_exception($e->getMessage(), $e->getCode());
}
For The People Wondering
I solved this by just adding a port to the host name like localhost:3308

PHP SQL Server Connection working or not working [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 4 years ago.
$serverName = 'servername';
$uid = 'username';
$pwd = 'password';
$conn = new mysqli($serverName, $uid, $pwd );
if (!$conn) {
echo "Connection failed: " ;
}
else
{
echo "Connected successfully";
}
This is my code. It gets connected to the database. I just want to confirm the code is right, because when i try doing this
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
it throws me connection failed error.
So, is my code connected to the server or not? because when i run a query it does not show me anything.
Code for the query:
$sql = "SELECT max([line_nbr]) FROM [dbo].[so_audit]";
$res = $conn->query($sql);
var_dump($res);
please advise
you are not trying to connect to sql server u should use sqlsrv_connect instead of mysqli
so u need to specify the server name and an array containin connection info for that your code should look like this :
$srv ="servername"
$info=array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $srv, $info);
if (!$conn) {
echo "Connection failed: " ;
}
else
{
echo "Connected successfully";
}

Connecting to mysql using php through xampp.... Database connection problems [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I've installed xampp and it seems to be up and running, I have tested php by executing the phpinfo() function and it works. I can create databases and manipulate them in phpmyadmin, and the localhost server works too
How ever when I attempt to actually connect through php....
<?php
$conn = mysqli_connect('localhost', 'root', '');
mysql_select_db("testskills");
if(!$conn) {
die("Connection Failed: ". mysqli_connect_error());
}
..... I don't get any errors but the code breaks and the browser just shows me the actual code from the file the form action called
I'm stumped
Lori
Try this
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
<?php
$host_name = "localhost";
$u_name = "root";
$u_pass = "";
try {
$dbh = new PDO("mysql:host=$host_name;dbname=personal_db", $u_name, $u_pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $ex) {
echo "Connection failed: " . $ex->getMessage();
}
?>
I would suggest that you start learning to use PDO man.

Error No Database Selected PHP/mySQL

I have read a bunch of the other posts about similar issues but I am very new to this so I am likely missing something. Most of the other questions I found were a lot more complicated than mine. I am trying to follow the w3schools tutorial for this and am testing locally using XAMPP. Right now I am just trying to get this to work successfully and eventually I am planning to submit data into a mySQL database from a web form.
<?php
$servername="localhost";
$dbname="mysql";
// Create connection
$conn = mysqli_connect($servername, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO owner_table (ownerFirst, ownerLast, mobile)
VALUES ('John', 'Doe', '1111111111')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
NOTE:
The connection is not failing from what I can tell. Removing everything after the first if statement, and adding else { echo "success" } outputs success. Again though, I am new to PHP.
If someone knows that the answer has been given in another post PLEASE PLEASE comment here and ill close this out.
http://www.w3schools.com/php/php_mysql_insert.asp
You should follow the proper syntax:
$conn = mysqli_connect(<host Name>, <username>, <password>, <database name>);
Reference: http://php.net/manual/en/function.mysqli-connect.php#refsect1-function.mysqli-connect-examples
Add two more parameters- username and password here:
$username = "root"; // Default values
$password = ""; // Default values
$conn = mysqli_connect($servername, $username, $password, $dbname);

Categories