Do this method can blow off the 2006 mysql error - php

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.

Related

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.)

PDO error handling and storing errors in database

How would I go about getting PDO statements to generate a safe error message? I don't want the user to see the error message. I want them to get directed to a page that says a clean message, "Whoops something unexpected happened!". I would also like to log the errors in a database to review and catch errors others are generating.
I'm using PHP and MySQL.
I found that when you make your connection you can set your error handling like this.
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Anyone do anything like this before?
So this is just a suggestion as I have never tried this but after thinking about it a bit I think it would be an interesting option to explore. As I am fairly new to PHP & PDO I'm sure there are other and better ways.
Perhaps you could try using the try function of PHP and then instead of echo'ing (if failed) the PDOException you could run another function that prints it to a text file. Something like.
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
foreach($dbh->query('SELECT * from FOO') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
$strFileName = 'whatever.txt';
if(!is_writable($strFileName))
die('Change permisions to ' . $strFileName);
$handle = fopen($strFileName, 'a+');
fwrite($handle, "\r" . $e->getMessage() . "\r");
fclose($handle);
}
?>
This way you would avoid a DB connection (which is the problem I guess) but still save the error.
You would perhaps want to omit the echo'd text after die within the if statement.
I think it is better to write your logs to a file, instead of a database. Especially since you want to log PDO errors, which indicate something is wrong with your database connection.
You can show the user a nice error page by catching your errors. You can redirect your users to your error page then, in case something went wrong.
You have to understand that PDO do not generate a "safe" or "unsafe" error message. It does generate an error message. That's all. The rest is is the responsibility of site-wide PHP settings.
PDO is not the only source of errors. Why care of PDO errors only? Why not to handle ALL errors the same way?
Want errors logged? It's a matter of one PHP ini setting.
Want errors not to be displayed? It's a matter of one PHP ini setting.
Want generic error page to be shown? It's a matter of simple function that will handle all errors at once.
Everything can be done proper and straight way, without wrapping every statement into try catch. Without writing into log manually. Without even single additional line of code.
You need to set up PHP error handling, not PDO.
And of course, it makes absolutely no sense in trying to store a database error in the same database that failed you right now. Errors have to go into error log on a live server and on screen - on a local development PC.
Anyone do anything like this before?
Sure. Every single one of 1000000s sites in the world. The way described above.

Mysql server has gone away - MySQL error 2006:

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);
}
?>

Why did my PHP stop working?

I have some complex code. Complex, but it was working.
The I wanted to add some new code, realized that something needed to become a function and then went on a refactoring rampage. Now my code no longer works.
So I did a bit of file compare, some code reading and debugging, and convinced myself that my changes hadn't broken anything.
To test this theory, I put together an exceedingly simple test program:
<?php
$connection = odbc_connect("Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=mysql;Option=3;", "root", "");
var_dump($connection);
echo '<br>';
$result = #odbc_exec($connection, 'show version()');
var_dump($result);
?>
which resulted in
resource(2) of type (odbc link)
bool(false)
The strange thing is that the odbc_connect() succeeds, but the simplest MySql command I can think of fails.
Btw, I have tested at the command line & the MySql server is up & running (Xampp) and reports v 5.1.41.
Obviously I am overlooking something very basic, but what?
This is the simplest MYSQL query I can think of:
select 1
This should help you determine if your connection is working or if the problem lies elsewhere.
Maybe the odbc driver "wants" to tell you something about what is causing the error...
$result = #odbc_exec($connection, 'show version()');
if ( !$result ) {
printf("error: %d %s", odbc_error($connection), odbc_errormsg($connection));
}
else {
echo "ok";
}
see http://docs.php.net/odbc_error and http://docs.php.net/odbc_errormsg

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