No database selected mysqli message [duplicate] - php

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.

Related

Not able to connect to MySQL database using PHP-FPM

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.");
}
?>

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

How to connect to local MySql database using PHP. [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
we are trying to connect to our local mysql database using PHP. We left out the password on purpose but is it correct. However, this is not working, we keep getting following error message: Warning: mysqli_connect() expects parameter 5 to be long, string given in.
It is referring to line 7 which would be $conn = mysqli_connect($servername, $port, $uname, $pword, $dbname);
<?php
$servername = "Mysql#127.0.0.1";
$port = "3306";
$uname = "root#localhost";
$pword = "1234";
$dbname = "database";
$conn = mysqli_connect($servername, $port, $uname, $pword, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>
Our confiq.php file
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'database');
/** MySQL database username */
define('DB_USER', 'root#localhost');
/** MySQL database password */
define('DB_PASSWORD', '');
/** MySQL hostname */
define('DB_HOST', 'Mysql#127.0.0.1');
define('port', '3306');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
You have the args in the wrong order in the constructor
$conn = mysqli_connect($servername, $port, $uname, $pword, $dbname);
should be
$conn = mysqli_connect($servername, $uname, $pword, $dbname, $port );
It looks however like the info in the config file is incorrect -
define('DB_HOST', 'Mysql#127.0.0.1');
would, I suspect, actually be
define('DB_HOST', '127.0.0.1');
though perhaps not!
so then, using the info from your config file, the connection would be:
$conn = mysqli_connect( DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, port );

Which database connection should be used to connect to MySQL with PHP, and why?

I have seen some ways to connect to the MySQL DB with PHP, but don't know why the one is better than the other, or safer or anything. Same thing goes with inserting or fetching data to/from the database.
This is some examples of MYSQL connection with PHP:
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'test');
$db = #mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die('Could not connect to database. '.mysqli_connect_error());
OR
class kobling extends mysqli {
public function __construct(
$host = "127.0.0.1",
$base = "test",
$user = "root",
$pw = "password",
$port = "3306") {
parent::__construct($host,$user,$pw,$base,$port);
}
}
if (!($db = new kobling())) {
die("Kunne ikke koble til databasen.");
}
Any suggestions or solutions?

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