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

I am trying to write php to connect data from myqsl (i use free host 000webhost.com)
file connect:
<?php
$connect = mysqli_connect("files.000webhost.com:21", "nguyendang", "password", "foodcourt_db") or die ("could not connect to sever " . mysqli_connect_error()) ;
mysqli_query($connect, "SET NAMES 'utf8'");
?>
I had these errors when i ran

Try hostname with localhost. And also check the MySQL server is currently running.
This may happen in hosting for less duration due to overload of MySQL Server

Try to use this connection:
$link = mysqli_connect("localhost", "username", "password", "database_name");
//check connection
if($link === false) {
die("ERROR could not connect. " .mysqli_connect_error());
}

Related

Connect to remotemysql.com on my website?

I want to connect a database on remotemysql.com too my website (ddu.nu/3/weatherstation). When i try to run the connection to the database on the website the connection fails. But when i try the exact same code on the localhsot the connection is created. Here is the code that i am using.
$connect = mysqli_connect("remotemysql.com:3306", "dbuser", "dbpass", "dbname");
if(!$connect ) {
die('Connection failed: ' . mysqli_error());
}
echo 'Connected successfully';
The website doesn't give any error.
Here is the website
And here is the localhost
My quetion is what I need to do in order for the real website to function.
You should try to disable ssl or the ssl have to have ssl cert, mine is work pefectly with ssl disable:
String dbURL = "jdbc:mysql://remotemysql.com:3306/";
Connection con = DriverManager.getConnection(dbURL + dbName + "?useSSL=false",
dbUsername,
dbPassword);

MAMP cannot connect to SQL server within a PHP script

I am quite new to the PHP world. I have installed MAMP on my Mac. This is the code I have written to connect to MySQL:
<?php
// Connect to the database server
$dbcnx = #mysql_connect("localhost", "root", “root”);
if (!$dbcnx) {
echo( "<P>Unable to connect to the " .
"database server at this time.</P>" );
exit();
}
?>
MySQL has already started and is running. I am not sure why am I not able to connect.
Try this
<?php
// Connect to the database server
$dbcnx = mysql_connect("localhost", "root", "");
if (!$dbcnx) {
echo( "Cannot connect to database due to ". mysql_error());
exit();
}
?>
Since you are getting Access denied for user 'root'#'localhost' (using password: YES) there is no need of giving password to connect with the database.So try with password ''.
Note:- mysql_* functions are deprecated as of PHP 5.5.0,and will be removed in the future.Instead, the MySQLi or PDO_MySQL extension should be used
Your code has smart quotes around the password. Try replacing them with regular quotes. If you have not changed the default MAMP password, this should work:
<?php
// Connect to the database server
$dbcnx = mysql_connect("localhost", "root", "root");
if (!$dbcnx) {
echo( "<P>Unable to connect to the " .
"database server at this time.</P>" );
exit();
}
?>

MySQL Server gone away in PHP

$mysqli = new mysqli("127.0.0.1:30", "root", "");
if ($mysqli->connect_errno)
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->
connect_error;
exit("..............");
}
This code executes a long time and in the end it says MySQL Server has gone away.
Apache Port is 30
MySQL server port is 3306
Using Microsoft Windows 8.1
you can also use this
$mysqli = new mysqli("127.0.0.1", "root", "");
mysqli.default_port=3306
this will connect by default through 3306 port.
check this

Error connecting PHP to SQLServer

I am using a script php that connect to sqlserver but it doesn't works.
//connection to the database
$dbconn = mssql_connect($Server)
or die("Couldn't connect to SQL Server on $Server");
if($dbconn)echo 'Connected';
//select a database to work with
$selected = mssql_select_db($DB, $dbconn)
or die("Couldn't open database $myDB");
![when running script it gives this error, i m using windows authentication][1]
http://i.stack.imgur.com/a9ndN.jpg
Check mysql_connect()
$dbconn = mysql_connect('hostname','username','password');
Connect using mysql_connect()
$dbconn =mysql_connect(servername,username,password);
example
$con = mysql_connect("localhost","me","123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

Connecting to a mysql Database via PHP?

Ok, Ive been playing around with PHP and databases. Note, Im doing all my work on a webserver hosted by bluehost.
So I went into the cpanel and set up a database called gagalugc_stocks.
I also setup a username (test) and a password (password) and added them to be able to control the databse. Below is my script in an attempt to connect to it, not it is also on the server.
<?php
$connect = mysql_connect('localhost','test','password');
if(!$connect){die('Could not reach database!');}
mysql_select_db("gagalugc_stocks", $connect)
?>
-Thanks
NOTE: The problem is that it can never reach the database.
<?php
$connection = mysql_connect ('localhost', 'test', 'password')
or die ('<b>Could not connect: </b>' . mysql_error());
$result = mysql_select_db("gagalugc_stocks")
or die ('<b>Could not connect: </b>' . mysql_error());
?>
Didn't check for port when adding database make sure to specify host and port
(localhost:2083)
How about
<?php
$connect = mysql_connect('localhost:2083','test','password');
if(!$connect){die('Could not reach database!');}
mysql_select_db("gagalugc_stocks", $connect)
?>
For local system:-
$con = mysql_connect("localhost","root","");
if (!$con){
die('Unable to connect to the server ' . mysql_error());
}
mysql_select_db("databasename", $con) or die(mysql_error());
For website or remote server:-
$con = mysql_connect("localhost","username","password");
if (!$con){
die('Unable to connect to the server ' . mysql_error());
}
mysql_select_db("databasename", $con) or die(mysql_error());
for more please visit : Algosoftwares

Categories