I have two database servers, MySQL and MariaDB, when I'm trying to connect to my local database on MySQL server, I always get "Connectionfailed: SQLSTATE[HY000] [1049] Unknown database 'ruff'". I found out, that my code is connecting into MariaDB server instead of MySQL. I can CREATE and CONNECT to any database on MariaDB.
How can I connect to MySQL server databases?
My code is:
// Creating a connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Creating a database named $dBName
$sql = "CREATE DATABASE $dBName";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully with the name newDB";
} else {
echo "Error creating database: " . $conn->error;
}
// Connecting to database named $dBName
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
if (!$conn)
{
die("Connection fail: ".mysqli_connect_error());
}
// closing connection
$conn->close();
Adding port number after the localhost solved the problem.
$servername = "localhost:3308"; //MySQL
$servername2 = "localhost:3306"; //MariaDB
Related
$servername = "localhost";
$username = "jaskaran";
$password = "India#123";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
jaskaran has all access mysql and when i try with
mysql -u jaskaran -p it is working fine by when i try with php it does not why
error is The server requested authentication method unknown to the client
even i try this also
ALTER USER 'jaskaran'#'localhost' IDENTIFIED WITH mysql_native_password
BY 'India#123';
I have bought a database MySQL to use on my domain/host.
I can connect to the database via wampserver (localhost) without problems.
But when I am trying to connect to the host website it gives me an error.
Connection failed: Can't connect to MySQL server on 'mysql-******.net' (111 "Connection refused")
My code:
<?php
$servername = "mysql-*********.net";
$username = "username8954";
$password = "dolphyn1235";
$dbname = "animals";
$dbServerPort = "26313";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname, $dbServerPort);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Any ideas how to solve this?
Check in your MySQL Server configuration files (my.cnf), find bind-address = 127.0.0.1 then comment them with # at the begining of the line. and then restar your MySQL.
I get this error when I try to connect to the mysql database using php mysqli class.
I am using
OS :- windows 10
webserver :- xampp
php :- 7.3.3
it got a warning error and error message is junk.
mysqli::__construct(): (HY000/2002):
�ڑ��ς݂̌Ăяo���悪���̎��Ԃ�߂��Ă������������Ȃ��������߁A�ڑ��ł��܂���ł����B�܂��͐ڑ��ς݂̃z�X�g���������Ȃ��������߁A�m�����ꂽ�ڑ��͎��s���܂����
Using following code:
$dbServerName= "*******ap-northeast-1.rds.amazonaws.com";
$dbUsername= "example";
$dbPassword= "*******";
$dbName= "example";
// Create connection
$conn = new mysqli($dbServerName, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
dbServerName is amazon.com for example,
password is * for security.
I am using 000 webhost to store a sql database.
Every time I want to connect to it I recieve this error message
Warning: mysqli::__construct(): (HY000/2002): Connection refused in /storage/h10/804/1186804/public_html/Handy_Help_PHP_files/conexiune.php on line 7
Connection failed: Connection refused
This is the php I use to connect
<?php
$servername = "localhost:3306";
$username = "id1186804_admin";
$password = "12345";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Can somebody help me to find the error?
with mysqli need add database name too
<?php
$servername = "localhost:3306";
$username = "id1186804_admin";
$password = "12345";
// Create connection
$conn = new mysqli($servername, $username, $password,$yourdbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
such:
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Remove port from host parameter and put in this order:
mysqli_connect(host,username,password,dbname,port,socket);
I am trying to connect to a MySQL database through a php script. It gives me this error from mysql_error(): Access denied for user '#localhost' to database 'userinfo'
userinfo is the database.
my script is this
<?php
$servername = "localhost";
$username = "root";
$password = "'mm'";
$database = "userinfo";
$conn = mysqli_connect($servername, $username, $password);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully<br>";
mysql_select_db($database) or die(mysql_error());
echo "connected successfully to db:" . $database . "<br>";
?>
you are connecting using
mysqli_
function
and selecting data with
mysql_
avoid using both at the same time. since they're incompatible.
use the mysqli_ alternative instead
mysqli_select_db($conn, $database);
and
mysqli_error($conn)
Please keep in mind this isn't the safest way. But since you have said your learning this it is a start.
<?php
$servername = "localhost";
$username = "root";
$password = "mm";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
http://www.w3schools.com/php/php_mysql_connect.asp
To select data from the database
http://www.w3schools.com/php/php_mysql_select.asp
It appeared you where combining the old mysql in php with the new mysqli