PHP not connection to MySQL server - php

I am trying to get my PHP pages to connection to a remote server hosting the MySQL database. I can connect via the command line just fine with the same username and password. Below is a simple test file I created. The only thing I've done to the code is remove the password, but I know that's not the issue. Like I said I can connect via the CLI. This is a Linux install. When viewed from the web browser, all I get is an error 500 page. If I comment out the mysqli statement, the page displays the Connected successfully.
<?php
$servername = "12.0.1.170";
$username = "jason";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
/ die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

Per the php documentation for mysqli_connect()
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
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($link) . PHP_EOL;
mysqli_close($link);
Your code appears to be lacking all four parameters required.
You have:
$conn = mysqli_connect($servername, $username, $password);
Perhaps try:
$conn = mysqli_connect($servername, $username, $password, $database);
with a $database='db_name'; value added into your variables section.

There are two possible issues with remote mysql connection:
1. Firewall of the server: You must enable incoming connection on port (for. e.g. 3306).
2. User in mysql: You must have a user created (for e.g. jason in this case)

Try connecting this way
$con = mysqli_connect('localhost','root','','db_name');
if($con)
echo "hell yeah its connected";
else
echo "i m boned"

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

Can't connect to localhost MySQL server with PHP

I have Apache (with PHP) and MySQL installed and running on my Raspberry Pi. I've done some simple tests with PHP, and it seems to be working. And MySQL is working perfectly from the terminal, and even another computer.
I made this PHP file:
<?php
echo("Connecting");
$connection = new mysqli("127.0.0.1", "admin", "password", "test");
if ($connection->connect_error) {
die("Connection error");
}
echo("Connection successful");
?>
Yet when I go to this page in my web browser, all I see is "Connecting". If I comment out the connection command, I see "Connecting Connection successful" in the browser.
It seems as if the PHP code stops running or hangs at the connection command.
Any ideas why I'm having this strange behavior?
Try adding these lines at the top: error_reporting(E_ALL);
ini_set('display_errors',1);
I hope You should do following
if ($connection->connect_error) {
echo "Connection error";
}else{
echo "Connection successful";
}
Try this instead
<?php
$link = mysqli_connect("127.0.0.1", "admin", "password", "test");
if (!$link) {
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($link) . PHP_EOL;
mysqli_close($link);
?>

PHP - If no MySQL Connection then display simple message

How can I catch and ignore errors when connecting to a database and display a simple text message instead?
My connection looks like this:
$connection = mysqli_connect($dbhost, $dbuser, $dbpw, $dbname);
If the connection fails I want it simply to echo "Error connecting to the database" instead of the big error box.
EDIT:
Although error_reporting(0) did work, what worked better was just putting '#' in front of mysqli_connect:
$connection = #mysqli_connect($dbhost, $dbuser, $dbpw, $dbname);
if (!$connection) {
$connection_status = 'Connection to database failed';
} else {
$connection_status = null;
}
?>
<?php echo $connection_status; ?>
Taken from the documentation of mysqli_connect:
Examples
Example #1 mysqli_connect() example
<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
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($link) . PHP_EOL;
mysqli_close($link);
?>
mysqli_connect:
$connection = mysqli_connect($dbhost, $dbuser, $dbpw, $dbname);
if (!$connection)
{
error_reporting(0);
die("Error: Unable to connect to MySQL." . PHP_EOL);
}
To disable "big error boxes" (I assume standart messages?), write error_reporting(0); above this.
You may set your custom handler for errors
register_shutdown_function("shutdownHandler");
function shutdownHandler()
{
if (!is_null($e = error_get_last())).
{
echo $e['message'];
}
}

Php MySQL Connecting a database via MySQL does not work but ODBC does

I'm trying to connect to a database via MySQL but it does not work. I'm getting the following error:
"Connection failed: No connection could be made because the target machine actively refused it. "
Here's the basic code:
<?php
$servername = "server";
$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";
?>
I can connect to the database from Excel and also via an ODBC connection with the code below but not via MySQL:
<?php
$user = 'username';
$pass = 'password';
$server = 'server';
$database = 'database';
// No changes needed from now on
$connection_string = "DRIVER={SQL Server};SERVER=$server; DATABASE=$database";
$conn = odbc_connect($connection_string,$user,$pass);
if (!$conn){
exit("Connection to the database Failed: " . $conn);
}
echo "Connected successfully";
?>
Any ideas?
You say you can connect with odbc which means you have an SQL Server database, not a Mysql database. If you don't like odbc, pdo is your other option. Mysqli is not an option as its mysql only.
Try this code:
<?php
$link = mysqli_connect('server','username','password','database');
if (!$link) {
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");
?>

Categories