Not able to connect to MySQL database using PHP-FPM - php

I recently installed php7.4-fpm (FPM/FastCGI). But I'm not able to connect to MySQL database.
I'm using the following configuration. Note my database has no password. Tried 'localhost', '127.0.0.1', 'http://127.0.0.1:3306', '127.0.0.1:3306', in DB_SERVER setting but no luck.
<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', '127.0.0.1');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'republic');
/* Attempt to connect to MySQL database */
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check connection
if($link === false){
die("Oops! Something went wrong. Please try again later.");
}
?>

Related

PHP Database connection error: Connection refused . Localhost works fine but real server error occurred

Php Confiq file
<?php
define('DB_SERVER','127.0.0.1:3308');
define('DB_USER','root');
define('DB_PASS' ,'*****');
define('DB_NAME','table_license');
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Error
Error Message
Using HeidiSQL- in Localhost database connection was successful but the server errors occurred?
Try "localhost" instead of "127...." for DB SERVER
Or try to connect without port, just ip
When defining the port for your mysqli connection, you have to use the fifth parameter (and not specify it in the host).
Also note that the default port is 3306, so if you're connecting to a shared host, its most likely on the default port (and not on 3308) - if that's the case, then you can omit specifying the port entirely.
<?php
define('DB_SERVER', '127.0.0.1');
define('DB_USER', 'root');
define('DB_PASS', '*****');
define('DB_NAME', 'table_license');
define('DB_PORT', 3308);
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME, DB_PORT);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli::connect documentation
You can try use:
define('DB_SERVER', '0.0.0.0');
Or
define('DB_SERVER', 'db-container'); // if use the docker

When the password is entered in the login it says that it is not valid but it works offline with no problems in php

When i am using laragon for developement of my website it does work the login normally but when i put on my server raspberry pi online always have a error for the password is incorrect. What could it be?
https://imgur.com/LwUuaIN
All code in this link for the php:
https://pastebin.com/pUQNBjpP
<?php
// Initialize the session
session_start();
// Check if the user is already logged in, if yes then redirect him to welcome page
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
header("location: admin_index.php");
exit;
}
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'xxxx');
define('DB_PASSWORD', 'xxxx');
define('DB_NAME', 'base_de_dados_museu');
/* Attempt to connect to MySQL database */
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
As this code relies on a MySQL databade running on the same server, the problem most likely related to the database (on the Pi) table not being populated with the correct users.

No database selected mysqli message [duplicate]

This question already has answers here:
Why do I get "No database selected" error message when executing SQL via mysqli?
(4 answers)
Closed 6 years ago.
i changed mysql codes in a project to mysqli but now i got message " No database selected" although all database information is right here is the code :
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root'); // Your database Username
define('DB_PASSWORD', 'root'); // Your database Password
define('DB_DATABASE', 'social'); // Your database Name
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysqli_error());
$database = mysqli_select_db($connection,DB_DATABASE) or die(mysqli_error());
mysqli_query($connection,$database);
mysqli_query($connection,"SET NAMES 'UTF8'");
mysqli_query($connection,"SET character_set_connection = 'UTF8'");
mysqli_query($connection,"SET character_set_client = 'UTF8'");
mysqli_query($connection,"SET character_set_results = 'UTF8'");
$path = "uploads/";
$LogoPaht="css/icons/";
$conversation_uploads = "conversation_images/";
$profile_image_path = "user_profile_uploads/";
$admin_path = "../".$LogoPaht;
$admin_profile_path='../'.$profile_image_path;
$admin_path_uploads='../'.$path;
$profile_cover_pic_path = "user_profile_cover_uploads/"; // User Profile Cover File
$perpage=10; // Updates perpage
$base_url='http://localhost/sociall/'; // base_url
$admin_base_url=$base_url.'smadmin/'; // Admin base_url
$gravatar=0; // 0 false 1 true gravatar image
$rowsPerPage=1000000; //friends list
$profilePerPage=3;
/*SMTP Details */
$smtpUsername='yourname#gmail.com'; //yourname#gmail.com or you can use your webmail like somename#yourwebsitename.com
$smtpPassword='pass'; //gmail password or your webmail password
$smtpHost='ssl://mail.yourwebsitename.com'; //tls://smtp.gmail.com if yo
$smtpPort='465'; //465
$smtpFrom='yourname#gmail.com'; //yourname#gmail.com}
?>
Remove the lines:
$database = mysqli_select_db($connection,DB_DATABASE) or die(mysqli_error());
mysqli_query($connection,$database);
Add the Database name to your connection establishment request by changing your code to this:
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root'); // Your database Username
define('DB_PASSWORD', 'root'); // Your database Password
define('DB_DATABASE', 'social'); // Your database Name
// specify the database name when establishing the connection
$connection =
mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die(mysqli_error());
/***
If your code dies before this comment, somethings wrong with
your credentials, the db name, or the db's not running on the host
you're trying to connect to
***/
// should be able to query away if connection succeeded...
mysqli_query($connection,"SET NAMES 'UTF8'");
...
...
If your connection is failing, it's could bebecause your mysql is setup for port access not socket access. Change the db line to test:
define('DB_SERVER', '127.0.0.1');
If this fails you're sure you're using a socket, keep it to localhost and verify you're user has localhost permissions to the mysql db.

can't connect db with xampp

in dbconect
<?php
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (mysqli_connect_errno()) {
echo " can't connect : " . mysqli_connect_error();
exit;
}
?>
and in dbconfig
<?php
define('DB_HOST', "localhost");
define('DB_USER', "root");
define('DB_PASS', "");
define('DB_NAME', "carwash");
define('DB_PREFIX', "carwash_");
?>
and output
Warning: mysqli_connect() [function.mysqli-connect]: (42000/1049): Unknown database 'carwash' in C:\xampp\htdocs\carwash\includes\dbconnect.php on line 2
can't connect : Unknown database 'carwash'
I don't know what's wrong or what's happened, it happen only today but the day pass it didn't happen
Make sure you have a database named carwash.
Xampp comes with phpmyadmin,have a look for the databases you have there.

cannot connect to database using mysql

I have a question that need all of you to help me. I got stuck with it several days. My problem is related to the connection to database using php. Both Mysqli and Mysql are enabled in phpmyadmin. I can connect to database through Mysqli, but i cannot connect through Mysql.
Here is code
Mysqli
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'xxxx');
define('DB_DATABASE', 'sample_db');
define('PORT', '80');
define('SOCKET', '/var/lib/mysql/mysql.sock');
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE, PORT, SOCKET) or die("Error " . mysqli_error($connection));
mysqli_set_charset($connection, "utf8");
?>
Mysql
<?php
define('DB_SERVER', 'localhost:/var/lib/mysql/mysql.sock');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'xxxx');
define('DB_DATABASE', 'sample_db');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die("Error " . mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
mysql_set_charset($connection, "utf8");
?>
Can you help me to solve this problem?
Thank in advance!
which type of error you see???
try to use instead of
define('DB_SERVER', 'localhost');
define('DB_SERVER', 'ipOfTheHost');
// data db
$username = "username";
$pass = "pwd";
$database = "nameDB";
$host = "ipServer"; //my is 62.149.150.187
// connection
mysql_connect($host, $username, $pass);
#mysql_select_db($database) or die("Argh... it's impossible to connect to it");

Categories