Strange Case Of MySql Server has gone away [duplicate] - php

This question already has answers here:
MySQL error 2006: mysql server has gone away
(32 answers)
Closed 8 years ago.
There is an interesting case that I would like to solve, I've used exception notification via email and got the following mail that says:
MySql has gone away for query 'INSERT INTO sl_usermeta ( USER_ID, CREATED,DESIGNATION ) VALUES ( 7695, '2014-06-02 16:20:48', 'Manager')'.
This query executes in millisecond when I run this through MySQL. However the interesting fact to be noticed is there is a entry in database with the following values
UserID----CREATED------DESIGNATION
7695------2014-06-02-----16:21:16
I've checked the logs and found the user did not send the same request again. I want to ask if MySQL retries the same query once it comes back after going away?

MySQL itself cannot have done, since you weren't able to contact the server and it is therefore entirely unaware of your INSERT statement in the first instance.
However, whatever client you're using may well be retrying. Since you didn't tell us what that is, it's impossible to say.

Related

Catch mysql_query function in PHP to create custom trigger [duplicate]

This question already has answers here:
How to override built-in PHP function(s)?
(2 answers)
Closed 8 years ago.
I have huge project in PHP, which is using mysql_connect function in lots of files.
My goal is to detect how many times mysql_connect was triggered from PHP. (from this project).
Idea is to not modify existing project files and create some parallel function (trigger) which will run only when this project will call mysql_connect in PHP.
Is there any way to configure PHP to create some trigger on mysql_connect? I mean, when PHP will call mysql_connect, this trigger must detect this and parallely run some another script, which will do counting or other stuff.
Any ideas?
There is no trigger event that is fired on new connections (at least none listed here: http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html)
But you can ask MySQL directly how many connections have been attempted (successful or not)
show status like 'Connections'
(See: http://dev.mysql.com/doc/refman/5.0/en/server-status-variables.html#statvar_Connections for more status information)
Maybe it also helps you to know that each connection gets a unique connection id which you can retrieve via
SELECT connection_id()
So if you have a table that is queried by each script your trigger could react to actions on that table, get the connection id of the active connection and write it somewhere if it has not been logged before.
Beware that the enumeration of connections starts at the beginning if the MySQL server is restarted. This also applies to MySQLs internal connection count which you can retrieve with show status.
But as already mentioned, I would also strongly consider to solve this problem on the side of PHP.

MySQL Last Insert and multiple web servers [duplicate]

This question already has answers here:
Is it possible for mysqli_insert_id to return an incorrect id in high traffic applications?
(5 answers)
Closed 9 years ago.
I am to the point now where I need to scale to multiple web servers plus a db server, instead of hosting mysql, and the site on the same box.
I use 'mysqli_insert_id' in php to return a key value for another following operation. As I scale out and more users hit the site, is this still a reliable method for getting the last insert ID? If for some reason, two requests hit almost simultaneously, is there any chance that using mysqli_insert_id could return the wrong insert ID since both inserts and select calls would happen seemingly instantly?
From the MySQL Documentation:
For LAST_INSERT_ID(), the most recently generated ID is maintained in
the server on a per-connection basis. It is not changed by another
client. It is not even changed if you update another AUTO_INCREMENT
column with a nonmagic value (that is, a value that is not NULL and
not 0). Using LAST_INSERT_ID() and AUTO_INCREMENT columns
simultaneously from multiple clients is perfectly valid. Each client
will receive the last inserted ID for the last statement that client
executed.
So yes, mysqli_insert_id will remain a reliable method for getting the last insert ID.

PHP/MYSQL issue - query search failing randomly

I have a PHP script designed to scrape data from websites. The script checks a locally-hosted mysql database each time it finds a new item to see whether or not that item has already been downloaded and already exists in the Mysql database. If it sees the item already exists in the database, it should ignore it and move on. This is the code I am using to do that:
$result = mysql_query("SELECT * FROM web_media WHERE sourceForum LIKE '%$ForumtoGrab%' AND titleThreadNum=$threadTitleExists");
if((!mysql_num_rows($result)) && (mysql_num_rows($result) !== FALSE))
{}
In other words, if it comes up with zero results, then that item is considered new. This script ran fine on my old hosting company for several months. I have recently moved to a new hosting provider, and I'm suddenly running into a very strange issue. Every 12 hours or so, the expression seems to randomly fail and the script finds a bunch of "new" data that already exists in the mysql database. I've tried running the query manually, and the code appears to have no problem finding the pre-existing entry no problem.
Does anyone have any idea what's going on here? I already checked with the hosting provider, and they say that the number of aborted Mysql connections we have is very low and isn't anything to worry about…so it doesn't seem like it's an issue with MySQL itself. I suspect it may be an issue with the mysql query?
Thanks
Try to check the MySQL error log for errors in your query.
PS: I hope you are preparing the $threadTitleExists for usage in a MySQL query (smth like (int)$threadTitleExists or mysqli_real_escape_string($threadTitleExists))

Migrating from php mysql to sql server php [duplicate]

This question already exists:
Closed 10 years ago.
Possible Duplicate:
SQL compatibility issue
I have asked 1-2 questions here at stackoverflow, but apparently i'm on the wrong way approaching!
The issue is, i got this thing. I connect to the sql server like this:
odbc_connect("Driver={SQL Server};Server=$host;Database=$database;",$uid, $passVal ) or die("Connection could not established");
It's supposed to use the database i've selected.
Now, here is how i do to select the table columns.
$result = mysql_query("DESCRIBE TABLE users");
$data = mysql_fetch_assoc($result);
print_r($data);
Now, please someone guide me where i'm wrong, it's my first approach with this.
You may try to act "sarcastic, ironic" all you want, it would be rude.
My question is, why doesn't it recognize the database? apparently, that's the issue.
Thanks
The odbc_ methods have nothing to do with the mysql_ methods. If you connect using odbc_connect, you need to use the odbc_ methods to query the database. Calling any mysql_ function makes it establish some default connection to the nearest default database, which is entirely separate from the previously established odbc_ connection.
Guess what, you cannot query an MS SQL server using the mysql API.

API connect to mysql sometimes not performing queries

a very puzzling and exhausting problem of mine:
I developed an API to allow others to draw information from my database. My server collects the POSTed info, writes up a mysql query, performs the query [$query = mysql_query($string, $connection);] , and returns the results (very simple).
the problem is that sometimes (say 1 out of every 5 tries) no info is returned. My server logs say that the resource ($query) is boolean (and therefore no results). My server receives the info from the remote users of the API every single time, the problem seems to be that my queries are sometimes just not being performed...
Why is this happening?
Is it a mysql performance issue? I never seem to have even a hint of a performance issue for queries made on my own page (i.e. not from the API)!
please help...
Your query might be failing. Try doing this:
mysql_query($string, $conn)or die(mysql_error());
If the query is generating an exception/error, it will stop the script and display the MySQL error. Using that error, you can fix your query so that everything will work fine eventually.
By the way, you are using $string, but it might be a better idea to use $builtQuery, because "string" might be confusing if you are going to need to edit the script later on.
Greetings.

Categories