Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm conecting to my online mySql database, the problem I am having is that the shared server I am on has a habit of timing out and dropping connections which is interupting my program. Is there a way to write in code, if this happens the program should continue? I am looking for either a 1 or a 0 to come back from the online database. I'm on GoDaddy. Here is the code. Thank you.

C++
(mysql_real_connect(conn,"xxx.db.xxxx.hostedresource.com","xxx","xxx","xxx",0,NULL,0) !=0);
char queryString[1024];
sprintf(queryString, "SELECT COUNT(*) FROM tblURLIP WHERE IP = '%s' AND IPStatus = '1' AND IPType = '3' AND IPMax ='0'",ipSrc == NULL ? "0" : ipSrc);
mysql_query(conn, queryString);
my_ulonglong i = 0;
res_set = mysql_store_result(conn);
my_ulonglong numrows = mysql_num_rows(res_set);
LEGIT = mysql_fetch_row(res_set);
Posted
Updated 8-Oct-11 9:29am
v2

1 solution

Back at your other question Unhandled Exception / Access Violation[^] I told you that you needed to handle all the error conditions and not just rely on the record set containing 0. Now you're back with the exact same program and still no handing of error conditions.

If you are doing calls, you need to realize that every call can FAIL miserably and you need to handle the failure return from the function. Only if it SUCCEEDS can you look at the data to see if the data says everything is OK. In other words, you need to deal with two types of FAILURES, 1) the overall operating system telling you that your call didn't work. 2) the application (mysql) telling you that the call worked but the data returns a different result than you expected. Deal with both.

Once you handle errors, you can decide how to make the program continue / recover or not.

Added an Example

The documentation for mysql_query says it can return non-zero if any number of errors occur, including CR_SERVER_GONE_ERROR or CR_SERVER_LOST. You'll need code like this (and don't try to paste it, I'm just typing it in, no compiler here)
int q_result = mysql_query(conn, queryString);
if (q_result != 0) // query failed
{
  // do something intelligent with the error case, you probably have to close and re-establish the
  // connection too and that might not work right away because the server may be rebooting and
  // that takes to to complete.  Also, there may be a local hiccup in the connection to the network
  // (damn Verizon - that's my carrier, you can bitch about your own provider in your own code).
}
res_set = mysql_store_result(conn);  // now that we know the query worked, lets get the results
if (res_set == NULL)
{
  // store result says there was an error, now I have to deal with that as well.  Why would the
  // query work and the store result fail?  Don't know, ask the mysql developers.  I'm just trying to deal with it.
  // do more intelligent error handling here
}
// now it's safe to pull data from the res_set using other mysql calls.
 
Share this answer
 
v2
Comments
Member 7766180 8-Oct-11 15:48pm    
OK, I get it. Thank you.
Chuck O'Toole 8-Oct-11 16:20pm    
Updated the solution to include some example.
Member 7766180 8-Oct-11 22:33pm    
Thank you so very much. It is very appreciated!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900