Warning: mysqli_connect(): Unknown MySQL server host - php

I have some trouble connecting to a mysql server. I can connect using the linux terminal, so I know that my host, port, user, and password works. I cannot, however, connect using PHP.
PHP Version: 5.2.17
Server version: 5.5.27-log MySQL Community Server (GPL)
Here is a test code example:
<?php
$link = mysqli_connect('host.com:5306', 'user', 'pass', 'db');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if (!mysqli_query($link, "SET a=1")) {
printf("Errormessage: %s\n", mysqli_error($link));
}
/* close connection */
mysqli_close($link);
?>
Gives the following warning:
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2005): Unknown MySQL server host 'host.com:5306' (1) in /.../test.php on line 3
Connect failed: Unknown MySQL server host 'host.com:5306' (1)
Any ideas on what to do?

The port number must be a separate argument:
$link = mysqli_connect('host', 'user', 'pass', 'db', 5306);

Any ideas on what to do?
Sure. A simple three-step solution for any php function's problem:
Open your favorite browser
type php.net/ in the address bar followed by a problem function's name:
`php.net/mysqli_connect` in your case
Hit Enter
Now you have the function's description and can check the proper parameters list.

host is the IP of your mysql server, or maybe "localhost"

The host parameter in the mysql function need to be "localhost" or "127.0.0.1"
You set the parameter as "host:port" and that is your mistake. It's need to be the full alias of the network in the server or the remote server with the full IP address. You can found more information about this error here: http://compnetworking.about.com/od/workingwithipaddresses/g/127_0_0_1_def.htm
The mysql book in php.net: http://www.php.net/manual/en/function.mysql-connect.php

Related

when i used to connect with phpmyadmin using netbeans in php programming language i got some error

This is my code for connecting to database:
$datbase=mysqli_connect("localhost:82","root"," ");
if(!$datbase)
{
die("connection failed".mysqli_connect_error());
}
it shows me that
Warning: mysqli_connect(): MySQL server has gone away in C:\Program Files\apache24\Apache24\htdocs\PhpProject3\php_DataBase.php on line 9
Warning: mysqli_connect(): Error while reading greeting packet. PID=10848 in C:\Program Files\apache24\Apache24\htdocs\PhpProject3\php_DataBase.php on line 9
Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in C:\Program Files\apache24\Apache24\htdocs\PhpProject3\php_DataBase.php on line 9
line number 9 is database=mysqli_connect("localhost:82","root"," ")
Error in this line and also remove space in password
$datbase=mysqli_connect("localhost:82","root"," ");
You are not connecting to MySQL, but to Apache server. If you didn't change MySQL port just use
$datbase=mysqli_connect("localhost","root","","databaseName");
Replace this line with
$datbase=mysqli_connect("localhost:82","root"," ");
with this one
$datbase=mysqli_connect("localhost","root","","your database name");
If your Mysql server is runs with default settings means . you don't need specify the port number . otherwise you need to specify port number as 5th parameter . like below Example .
Mysql default port number is 3306
$datbase=mysqli_connect("localhost","username","password","database_name","port_number");
if(!$datbase)
{
die("connection failed".mysqli_connect_error());
}
You should read first http://php.net/manual/en/function.mysqli-connect.php.
$link = mysqli_connect("host", "my_user", "my_password", "my_db");
Try this:
$datbase=mysqli_connect("localhost","root","","my_db");
if(!$datbase)
{
die("connection failed".mysqli_connect_error());
}
you are using port number on localhost that you are not set in php.ini file
try like this
//$datbase=mysqli_connect("localhost","username","password","databaseName");
$datbase=mysqli_connect("localhost","root","","databaseName");

Mysql Operation timeout Error

Warning: mysqli_connect(): (HY000/2002): Operation timed out in
/Applications/XAMPP/xamppfiles/htdocs/conn.php on line 4
Connection failed: Operation timed out
Get the following error while connecting to XAMPP on Mac OS X.
Code line is
$conn=mysqli_connect("SNA","root","","localhost");
// Check connection
if (mysqli_connect_errno()) {
echo "Connection failed: " . mysqli_connect_error();
}
Is it a Mysql Host problem or a Mysql default socket problem and how to resolve it? Default Host is 3306. I dont know where my mysql.sock file is generated and whether its generating or not.
change your code here, interchange host and database name
$conn=mysqli_connect("localhost","root","","SNA");
check php manual for more info : http://php.net/manual/en/function.mysqli-connect.php

mysqli_connect(): (HY000/2002) failed to connect, remote server [duplicate]

This question already has an answer here:
SQLSTATE[HY000] [2002] A connection attempt failed.. - When attempting to connect from Local to remote server
(1 answer)
Closed 3 years ago.
I am trying to connect to a remote server database using PHP code (mysqli)
$host = 'myIP:3306';
$user = 'root';
$pass = 'pass';
$conn = mysqli_connect("$host", "$user", "$pass");
//check connection
if (mysqli_connect_errno())
{
echo "Error: " . mysqli_connect_error();
}
I used myIP/phpmyadmin to make a database and i used putty to check if mysql was running correctly. I also checked the default port for mysql on my server, which was the default (3306).
The error given is described as either a connection fail/time-out/no response.
I also tried using PDO instead of mysqli but it gave me the same results. All other examples i could find where about localhosts and the solution given in those posts is using the socketpath to connect (which doesn't seem like a solution for me as it's remote..).
It's a linux server provided by school.
Any ideas? Thanks in advance.
With mysqli_connect(), you can use 6 parameters:
host, username, password, dbname, port, socket
Host and Port are separate.
Try using:
$conn = mysqli_connect("myIP", "$user", "$pass", "dbname", "3306");
Also, the default port is 3306 so you probably do not even have to provide it.

I get connection error when I add my database name in connection

Warning: mysqli_connect(): (42000/1044): Access denied for user '-----'#'localhost' to database '----' in ---.php on line 4
Unable to connect to MySQL
$db = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die("Unable to connect to MySQL");
This is how I connect. If I remove DB_NAME from there, I don't get an error and connect database but whenever I add DB_NAME, I get this error. DB_NAME is definitely correct but I don't see why? By the way for DB_HOST, I use localhost.
---Solved---
I finally solved the problem. So, for those of you who have the same problem, I am gonna explain everything. Actually, I decided to use table name before my query and than mysqli gave me another error saying something like "INSERT command denied to user ..." So, I opened up phpMyAdmin and changed my DB_USER to the name that is written in Database Server section in phpMyAdmin. You will see something like this;
Server: Localhost via UNIX socket
Server type: MySQL
Server version: 5.5.42-cll - MySQL Community Server (GPL)
Protocol version: 10
User: -HERE-
Server charset: UTF-8 Unicode (utf8)
So you need to use use the username, which is written in User part. After I changed my DB_USER I was able to add DB_NAME. Basically all the problems I was having (not being able to use DB_NAME and not being able to use sql commands) caused my permission.
The key part is; I was using DB_NAME, which I used to create database in my server and it also let me connect to the database but As I can see it is not the root. So, if you are having "denied" error, make sure that you check the values you typed in mysqli_connect!
You can use
# mysqli_connect(hostname, username, password, database)
or
#mysqli_connect(hostname,username, password)
# mysqli_select_db(true, database);
or
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno())
{
echo "Failed : mysqli_connect_error();
}
mysqli_select_db($con,"db");
if you are using localhost then try this
<?php
$con = mysqli_connect("localhost", "root", "", "YOUR_DB_NAME");
if (mysqli_connect_error()) {
die ("Could not Connect");
}
?>
and if you are doing this online then replace localhost with host_name root with username "" with password and at last your DB_Name. I hope this would solve your problem :)

Why is my php code not connecting to my remote MySql database?

I'm trying to connect to a remote MySql database and I get this error message:
Warning: mysqli_connect(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\myLocalDiectory\RemoteConn.php on line 9 "Resource not found" error 404.
Here's my php file that I'm trying to use:
<?php
define("DB_SERVER", "serverName");
define("DB_USER", "userName");
define("DB_PASS", "password");
define("DB_NAME", "dbName");
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
Am I missing something? Thanks for any help!
The first thing that I would check (if you haven't done so) is that you can in fact connect to the database from the computer that runs your PHP script. This to rule out a network or firewall problem.
The first thing would be pinging the server. In a DOS prompt run:
ping servername
Where "servername" is the same string that you put in your PHP script above. If this does not reply with a string similar to the one below, specifically, the first word is not "Reply":
Reply from 192.168.239.132: bytes=32 time=101ms TTL=124
This means that there is most likely no connectivity between the computer running the PHP script an the mysql server. I would then check if the server and the computer are properly connected to the network, if the server is up, an if there is not firewall in your computer running the PHP script or on the server.
Now, if your test above shows "Reply" to the ping, you can test if you can connect to the Mysql service from your php server. For this you can use Mysql workbench (http://dev.mysql.com/downloads/workbench/) and from there create a connection with the database parameters that you are giving to your script. If you cannot connect with Mysql workbench, you might need to disable a firewall in your Mysql server, a firewall in your computer running PHP, or enable the Mysql server to accept remote connections for the database and username that you use in your PHP script (some distributions Mysql server are installed to only accept local connections for safety).
If the problem is a permission in the server (the user can only connect locally but not from a remote computer for instance), you can enable the permission in the Mysql server with the GRANT command: http://dev.mysql.com/doc/refman/5.1/en/grant.html
define("DB_USER", "userName");
Try root instead of userName and define DB_HOST.

Categories