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.
Related
I came across in a project of about 900 files with 3 millions lines of code, and my boss asked me to find a solution to prevent mysql_error() to show errors.
This is the syntax
mysql_query() OR die(mysql_error())
So, how can I disable the mysql_error() from showing errors?
function mysql_own_error($debug = false){
$msg = mysql_error();
if($debug){
echo $msg;
}else{
// Function to log errors here.
}
}
With that you can set a global debug-variable $debug. Only if that is true output the error msg.
Now replace every mysql_error() with mysql_own_error($debug). There are Editors that can do suche replaces fast.
With that you will prevent the mysql_error() from showing errors publicly but you can still debug the code if you need to.
If you're still having errors then your project is not finished yet.
One way would be to do a site-wide find/replace on your files and replace the OR die(mysql_error()) with an # in front of mysql_error() like so: OR die(#mysql_error()).
Placing an # in front of a function call suppresses error messages. But use it carefully, this is not always a good solution.
Read this post which links to this article to know if it's a good solution for you.
I would change all OR die() occourrences to a custom error-handling function, then if you get an error you will still know about it without displaying them to users.
Yes, it would take a lot of time, but a good project takes a lot of time.
Check this article to create your own error-handling function and this other one to Enable PHP error logging via .htaccess, they really helped me.
I have built a query ($query_q = "SELECT * FROM `table`;") and am trying to execute it within a function.
public function read_from_table() {
$query_q = "SELECT * FROM `table`";
$query_a = mysql_query($query_q);
while (mysql_fetch_array($query_a)) {
echo "Did it!";
//OR AS TRIED ANOTHER WAY
return "Did it!";
}
}
And called as such:
echo $classInstance->read_from_table();
//OR AS TRIED ANOTHER WAY
$classInstance->read_from_table();
Both the ways that the function and the class have been made have been tried in every conceivable way, and yet I still get no result.
I was getting the error that says the xecution time has exceeded the limit of 30 seconds, so I added the ini_set('max_execution_time', 0); (knowing this removes time limit altogether) to see if the query would execute at all, it has been running now for 30 minutes without a sign of life. Why is the query not executing?
Additional comments:
I am aware that I am using the depreciated mysql_* functions, this is at client request and will be updated after the site has been made live and is complete to a point where I am ready to change it all to mysqli->* functions.
The table that I am calling (it's name has been stripped and replaced with `table`) has only 9 rows in it, so this should not affect the execution time greatly (or will it?).
I have had to strip all sensitive information from the function to satisfy the client and my employer. Please keep in mind that I cannot disclose and information that the client and my employer do not wish to disclose.
The issue was that the internet and server had gone down.
This has since been sorted and is operational.
Thank you for help and support in this.
DigitalMediaMan
try
error_reporting(E_ALL);
if all ok, try run this query from console, look, how many times query will be performed
before this, kill old process in database(show processlist and kill pid)
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.
I am trying to talk to a remote MongoDB server from my system. The code i use is like this
$con_string='mongodb://server_ip:27017';
$m = new Mongo($con_string);
$temp=$m->selectDB("DBName");
try {
$mc=$temp->collection_name->find()->limit(5);
var_dump($mc->info());
var_dump(iterator_to_array($mc));
}
catch (MongoCursorException $e) {
echo "error message: ".$e->getMessage()."</br>";
echo "error code: ".$e->getCode();
}
Now i get the following message from Xdebug
MongoCursorException: couldn't send query: ët§ôï9·H'ﯤ7·ø?u§Ht§ ö·Ìu§®u§Ì½u§4e
Why is this exception raised and why is there junk at the end of exception. I dont get this exception every time but 5 out of 6 times.
Also the message i got from catch block is
error message: couldn't send query: QY¥ôï9·H§ï³¤7·DCY¥h §
error code: 14
The error code 14 means "C socket error on send." says the PHP manual.
What does this error mean?
A guy in chat suggested that the junk is indicator that the data might not be utf8_encoded but i am doing a simple find() with no criteria so what do i need to encode?
EDIT:
To get around this scenario i wrote this
function getCursor($conn,$db_name,$coll_name,$query_options=array(),$fields=array(),$cursor_options=array(),$max_tries=0) {
$counter=0;
while(1) {
try {
$cursor=new MongoCursor($conn,$db_name.'.'.$coll_name,$query_options,$fields);
if (array_key_exists('count',$cursor_options))
return $cursor->count();
if (array_key_exists('limit',$cursor_options))
$cursor=$cursor->limit($cursor_options['limit']);
if (array_key_exists('skip',$cursor_options))
$cursor=$cursor->skip($cursor_options['skip']);
if (array_key_exists('sort',$cursor_options))
$cursor=$cursor->sort($cursor_options['sort']);
return $cursor;
}
catch (MongoCursorException $e) {
$counter+=1;
if ($max_tries>0) {
if ($counter>$max_tries)
echo "error message: ".$e->getMessage()."\n";
echo "error code: ".$e->getCode()."\n";
return false;
}
}
}
}
The functions takes the query parameters and then send the query to the server and if the MongoCursorException is raised it sends the query again. It does this $max_tries times. If $max_tries is set to 0 it will keep on sending this query to the server until the query succeeds. Maybe this is stupid, i should have set this to some fixed value like 50. On success the function returns the cursor except when your looking for the count and in that case it returns the count
Your string looks like that if:
You have a charset encoding issue
Memory is broken
As in your code not much strings are involved, the chances that this is an encoding issue are not very high.
But as Xdebug is a PHP extension that deals a lot with internals, breaking memory can be one thing.
An easy way to find out is to disable the extension you suspect that is causing the issue, e.g. xdebug.
It is also useful for documentation purposes tp write down which PHP and extension versions you are using as well as on which operating system. I asked for that in comments, it's a list like:
PHP 5.3.10
Mongodb Extension 1.2.10
Mongodb Server 2.0.6
Xdebug 2.2.0
Centos 6.2 64-bit
Checking the website for xdebug shows that there is a new version available. As you have found out earlier by disabling xdebug that it influences the result, try with updating the xdebug extension and see if it helps.
Even if it does, keep your version list for further reference (as well with a short description of the problem), because in case the problem is not fully solved with the upgrade, this information can be very useful at a later time to do a useful bug-report. Those internal problems in the software are sometimes hard to spot, so the information can help to reproduce easier then or to identify the areas involved.
This is fixed in Mongo driver v1.3+
ref: https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/IJ1n_Xt_al8
Downloads: http://docs.mongodb.org/ecosystem/drivers/
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);
}
?>