Mysql server has gone away - MySQL error 2006: - php

When trying to edit table data in one of databases, I can't apply the change because of the error
"MySQL error 2006: mysql server has gone away"`
This problem is intermittent. So I did some research on this and I came across this post. (note: I'm not at all knowledgeable on databases and php). Now I see mysql_ping Pings a server connection or reconnects it if there is no connection.
Sounds great. My issue is how do I apply this mysql_ping? Where do I go and do it? Is it ok to apply it or will it effect certain things?
My server runs off Windows 2003, IIS and I have PHP 5.3.8. I've had a look here but I'm battling to understand it.

Make a subroutine/function that includes mysql_ping, and use it insted of mysql_query.
For example
<?php
function my_query($sql)
{
if(!mysql_ping())
{
if(!mysql_connect( /* add connection parameters here */ ))
{
trigger_error("Database not avaible: " . mysql_error());
return FALSE;
}
}
return mysql_query($sql);
}
?>

Related

How to see LDAP errors generated from PHP

I'm trying to bind to a remote, third party LDAP server. I can connect/bind/search just fine from the command line on my server, but PHP just gives the monumentally unhelpful error: "Cannot connect to server".
I've seen where I can set the log level, but I don't know where those errors actually get logged to. Searching along the lines of "ldap [client] log location" returns results exclusively for ldap servers
How do I see the errors PHP generates when trying to bind?
If it matters, I'm using Debian.
Edit
Here's the relevant code:
public function auth($username,$password){
$ims_config = $this->di()->get('config')->ims;
$username = $this->filterUsername($username);
$this->conn = ldap_connect($ims_config->url);
if($this->conn){
$rdn = str_replace('?', $username, $ims_config->bind_rdn);
if(ldap_bind($this->conn,$rdn,$password)){
$this->isAuthed = TRUE;
}
}
return $this->isAuthed;
}
I have checked $ims_config->url and $rdn, and they are correct.
This is a pretty old question and the poster probably found the answer already, but for the sake of answering and completion, the PHP LDAP technote explains how you can do this using ldap_error($conn) and ldap_get_option($conn, LDAP_OPT_DIAGNOSTIC_MESSAGE, $err).

MySQLi Transaction Server gone away

I use the following code to insert some orders in the DB with a mysqli transaction. Sometime it works without any problems and sometime it just throws an error after 1 minute saying: "Mysqli Server gone away". When I trigger the same thing just after the error it just works.
The php file is trigger via jquery post.
function InsertOrder($children){
global $mysqli;
foreach($children as $child){
InsertOrder();
}
}
require('../../sys/connect.php');
$mysqli->autocommit(FALSE);
$error = false;
foreach($_POST){
InsertOrder($children);
}
if(!$error)
{
$mysqli->commit();
die("done");
} else
{
$mysqli->rollback();
die(getLanguageKey("label_mysqli_rollback",array($error)));
}
It's just a light version of my code, but that's it.
If I remove "$mysqli->autocommit(FALSE);" it works all the time.
It also works in other recursive functions I use.
You can log in at ur.ready2order.at with the demo account button. Then you can call:
http://ur.ready2order.at/views/order.dev.php?t_id=155
This site uses the mentions script. Sometime mysqli server gone away...sometime not.
Clicking the white button puts the product to order list and blue part of button opens the sidedishes and comment function.
Thanks in advance
There is documentation about it: MySQL server has gone away
Try to change your wait_timeout or increase speed of your script.
I found the issue. As I rebuild my code from using mysql to mysqli, I forgot to rewrite a small function. This caused the error because it was still looking for mysql().
Sorry guys and thanks.

Condition in the error: 'mysql has gone away'

I have a server and it performs actions every 10 seconds, but sometimes, it returns the error 'mysql has gone away'.
I wonder when this error is returned for the page to be refreshed.
I tried the following way but it did not work:
$remote_db = mysql_pconnect($remote_db_host, $remote_db_user, $remote_db_pass) or die (mysql_error());
if(!$remote_db) {
//error on connect
echo '<meta http-equiv="refresh" content="1">';
}
mysql_select_db($remote_db_name, $remote_db) or die (mysql_error());
That's a timeout message. Your PHP code took too long to do anything with the MySQL server and therefore it went away.
The issue is that the persistent connection you are using have died.
So, stop using mysql_pconnect and switch to mysql_connect.
For most cases it is actually speedier to do a normal connect than to try to use a persistant connection.
(And as a side-note: you should really take a look at PDO. mysql_ usage is strongly discouraged.)

Do this method can blow off the 2006 mysql error

Few days back i suddenly came up with getting empty result, I was surprised and then check out the mysql_error function and then got this 2006: Mysql server has gone away. The problem is that it doesn't comes everytime, but sometimes only for the same query.
I have checked out many questions here at stackoverflow as well as many blogs on the web, but I found no possible solution for this error
Today, I wrote this function for that and the error gones away,
$conn = mysql_connect($this->host, $this->dbUser, $this->dbPass);
$db_select = mysql_select_db($this->dbName, $conn);
for($i=0;mysql_error($conn) != 2006;$i++)
{
$result = mysql_query($sql, $conn);
if($i > 10){break;}
}
I wanna ask the professionals that if it it is the proper way of getting rid of this kinda situation or it is the wrong method for that
This error will occurr when the MySQL server has been restarted / unavailable. It could be happening on a specific query if the query is causing some kind of problem (can't say more without knowing what the query does).
Either way, this error is valid and shouldn't be ignored. You need to find out WHY it is being raised in the first place.
You can use the mysql_ping function to check if the server is available before running a query rather than catching the error.

How do I test for an Oracle connection

I'm trying to connect to an Oracle DB which is currently offline. When it's online it's not a problem, however, now that it's offline my program is getting hung up on the $connection = oci_connect() line and timing out. How do I simply check the connectio and bail out if it's not there?
Try this (fill in your ip and port):
if ( #fsockopen($db_ip,$db_port ) ) {
//connect to database
} else {
// didn't work
}
This gives you both a manual error, plus will return the actual error.
$connection = oci_connect() or die("Critical Error: Could not connect to database.\n\n". oci_error());
Make sure to test this as hopefully the Oracle error doesn't do something stupid like return the connection string (with your DB password) but I wouldn't assume until you see for yourself.
You could select null from dual.
OK, now I see what your asking, I think.
You want to know how to tell if a database is up before you connect to it?
You can use TNSPING to see if a database is up... ok, maybe that's not 100% accurate but it's a good indicator. go to a command prompt and type TNSPING and hit enter. So then you have to figure out how to call a command line tool from PHP.
Here is what I do in ASP.NET
Dim OracleConn As New OracleConnection(YOUR CONNECTION STRING HERE)
Try
OracleConn.Open()
OracleConn.Close()
Catch ex As Exception
Session("ErrorMessage") = "OracleConn: " & ex.Message
Response.Redirect("AccessDenied.aspx")
End Try
It doesnt necessarily say the DB is offline, but an exception will occur if the connection cannot be opened

Categories