Cant connect to mysql: MySQL server has gone away - php

I'm start to learn mysql not so long time ago, and now i have problem, that i cant solve by myself.
I'm trying to connect to mysql server with php. My admin gaved me ip, port, login, pass and db name.
Php file looks like this:
$hostname = '192.168.1.20:1433';
$username = "h";
$password = "h";
$dbName = "mydb";
mysql_connect($hostname,$username,$password) OR DIE(mysql_error());
mysql_select_db($dbName) or die(mysql_error());
$query = "SELECT * FROM `insy` WHERE 1;";
$result=mysql_query($query) or die(mysql_error());
echo "works!";
mysql_close();
When i run page, its do nothing few minutes, than errors appears:
Warning: mysql_connect() [function.mysql-connect]: MySQL server has gone away in N:\home\192.168.1.19\www\phlib.php on line 12
Warning: mysql_connect() [function.mysql-connect]: Error while reading greeting packet. PID=1908 in N:\home\192.168.1.19\www\phlib.php on line 12
Warning: mysql_connect() [function.mysql-connect]: MySQL server has gone away in N:\home\192.168.1.19\www\phlib.php on line 12
MySQL server has gone away
12'th line is:
mysql_connect($hostname,$username,$password) OR DIE(mysql_error());
I tried to create ODBC Sql Server via Control Panel -> Administration - with same parameters - it works, connection is ok, and i ca see the Db's.
Tell me please, where is my fault?
I hope, i was clear.
Thanks for help.

you can try mysql_ping($conn) if your server connection lost.
http://php.net/manual/en/function.mysql-ping.php

I've just solved that problem.. the thing was when I did create a new user in MySql, after writing the user, the server ask you for where will be the "server" and you put localhost:8080.. ERROR! don't put the port!! just localhost.
And thats how I solved it..

Related

mysqli_connect(): (HY000/2006): MySQL server has gone away

I get this error:
Warning: mysqli_connect(): MySQL server has gone away in C:\Apache24\htdocs\soccer.php on line 5
Warning: mysqli_connect(): Error while reading greeting packet. PID=19176 in C:\Apache24\htdocs\soccer.php on line 5
Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in C:\Apache24\htdocs\soccer.php on line 5
Fatal error: Maximum execution time of 30 seconds exceeded in C:\Apache24\htdocs\soccer.php on line 5
when trying to run a php file on localhost.
php7.2 MySQL 8
i have tried alot of suggested soltutions on for hours but no success.
here is the PHP code which i am just using for other purpose and dont understand all of it but just because i want to connect a database to may Application
<?php
$con = mysqli_connect("localhost:81","root","root","testdb") or die ("could not connect to mysql");;
if(mysqli_connect_error($con))
{
echo "Failed to connect";
}
$query = mysqli_query($con,"SELECT * FROM testtb");
if($query){
while($row=mysqli_fetch_assoc($query))
{
$flag[] = $row;
}
print(json_encode($flag));
mysql_free_result($query);
}
mysqli_close($con);
?>
Mysql Server use 3306 port as a default. You are trying to connect with Mysql 81 port and Mysql refused to connect with that port. So change
$con = mysqli_connect("localhost:81","root","root","testdb") or die ("could not connect to mysql");;
to
$con = mysqli_connect("localhost:3306","root","root","testdb") or die ("could not connect to mysql");;
By the way yo don't need to write port. 3306 is default.
$con = mysqli_connect("localhost","root","root","testdb") or die ("could not connect to mysql");;
well for some reason i used 127.0.0.1 instead of localhost and worked Thanks anyway for the help.
if you are using xampp then check the port number of mysql server and change
this ---->>
$con = mysqli_connect("localhost:81","root","root","testdb")
to ---->>
$con = mysqli_connect("127.0.0.1:3306","root","root","testdb")
In the place of 3306 you can put your mysql server port number.

Connecting MySQL database to website with PHP

As the title indicates I'm trying to link my MySQL database to my website using php.
I'm using GoDaddy hosting for the MySQL database and I got this code directly from them and it's still not working for some reason, giving the error below.
Here's my code :
<?php
$hostname = "trdlibrary.db.6253425.hostedresource.com";
$username = "*******";
$password = "*******";
$dbname = "trdlibrary";
$connect = mysqli_connect($hostname, $username, $password, $dbname, 3306) OR DIE ("Unable to connect to database! Please try again later or contact an administrator for help.");
?>
Here's the error :
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:\xampp\htdocs\searchengine\dbconnect.php on
line 6 Unable to connect to database! Please try again later or
contact an administrator for help.
Can anyone see the issue?.. or is this something I need to contact GoDaddy's support line about.
-- EDIT --
I forgot to mention that I am running the web server off my local machine using XAMPP (so I can run the PHP scripts) but the MySQL server is off-site with GoDaddy, so 'localhost' won't work currently.
-- EDIT #2 --
So I learned that sadly, remote access of MySQL databases to GoDaddy requires a more expensive and 'higher tier' hosting package from them. So I just answered my own question, thanks to those who replied.
You have to setup remote access to databases on Godaddy. You can refer to this page to see how to enable it.

Connection to MySQL server lost (error:111)

I have a php file to fetch data from a server
When I try to execute it, I get a message saying:-
Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in /home/-/-/public_html/GetData.php on line 3
And line 3 is
$con = mysql_connect("cs.neiu.edu","myusername","mypassword");
Also I've tried by giving the hostname as localhost,but its not working.
You need to configure binding address bind-address in my.cnf and grand access to the database eg. GRANT ALL ON foo.* TO bar#'162.54.10.20' IDENTIFIED BY 'PASSWORD';
That error means connection refused. I am in the same class that you are and obviously I am using the same server, and I would guess that you are entering the wrong password. It may not be the same password that you connect to the server with if you changed your password from what was given in class. I use the following and it works fine....
<?php
$con = mysql_connect('localhost','username','password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databaseName", $con);
..
..
..
mysql_close($con);
?>
Actually i realized that my password was wrong.

Having trouble with connecting to MySQL database in php

I've made mysql.php file which contain codes to connect to my database.
However, i'm getting this error message over and over again.
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'localhost' (10061) in C:\CustomerData\webspaces\webspace_00290195\wwwroot\mysql.php on line 7
Warning: mysql_select_db() [function.mysql-select-db]: Can't connect to MySQL server on 'localhost' (10061) in C:\CustomerData\webspaces\webspace_00290195\wwwroot\mysql.php on line 8
Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in C:\CustomerData\webspaces\webspace_00290195\wwwroot\mysql.php on line 8
Unable to select database
I've checked if my username, host, database, and password were misspelled, but they were all correct.
I've looked around the internet, but couldn't seem to find the right one for my problem.
My mysql.php file contains:
<?php
// Mysql settings
$user = "example_example";
$password = "example";
$database = "example_example";
$host = "localhost";
mysql_connect($host,$user,$password);
mysql_select_db($database) or die( "Unable to select database");
?>
I've changed my user, password and host name to "example".
I just couldn't find the solution for myself, and I need your kind help.
Thank you in advance.
Please try to find out the error by using mysql_error() function as follows :
mysql_connect($host,$user,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
and then try to find the exact reason for the failure.
Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'localhost' (10061)
here a list of questions you have to do when this error appears:
1.- is mysql running on the server?
1.1.- is mysql running on default port or in a custom port?
1.2 - is mysql accepting connexions (from localhost, from an ip...?
1.3 - your firewall is blocking mysql?
2.- the database exists on the server?
3.- the user:password have access to that database?
3.1- the user:password can log in from localhost, from an ip... from any place that mysql
is accepting?
I think I don't forget anything
Please check first that your mysql server is running properly.
Mysql settings
$user = "example_example";
$password = "example";
$database = "example_example";
$host = "localhost";
$cn=mysql_connect($host,$user,$password);
mysql_select_db($database,$cn);
Please try this it may work
I am not expert of php but this code is working perfectly in my website
if you are getting any error after applying this code then please inform me i will rty to solve the problem.
Thanks
You should provide the connection that need to use from database whle selecting database. Also use single quote instead of double quotes.
<?php
// Mysql settings
$user = 'example_example';
$password = 'example';
$database = 'example_example';
$host = 'localhost';
$link = mysql_connect($host,$user,$password);
mysql_select_db($database,$link) or die( "Unable to select database");
?>
Reference
You have to get the database information first. You posted this problem when you try to connect to your database in your website hosting, right?
So, go to your hosting control panel to get the MySQL database (and take note of where the database is stored on! Sometimes some hosting providers don't use "localhost". Instead, some of them may use 'serverXX.yourhostingprovider.tld' where XX means the server ID), and then change the corresponding variables with the value you've obtained from the hosting control panel.

PHP & MySQL Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2005): Unknown MySQL server host

Okay when I go to my web page I get the following error below but when I reload the page everything is okay and is displayed fine. What exactly is the problem and how can I fix it?
ERROR.
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2005): Unknown MySQL server host
MySQL code.
$mysqli = mysqli_connect("example.com", "avd", "password", "avd");
Server/Port combination is invalid, can't be resolved, or port is bad.

Categories