Ok, this is a tiny Snippet, and to be honest, I'm only posting it to see If i
can do anything better.

Terms of Use:
	email me, saying your using it. Or not, it doesn't matter, this is too minor
	to matter really, but my ego could use a boost ;)

If, you find any bugs, problems, "you shouldn't"s PLEASE, tell me, the handler functions
I post here are used in numerous things I do... So its important ;)
cyhawkx@gmail.com
-Thri


Any comments, suggestions on MySQL in general are extremely welcomed, since I'm learning
this from scratch and confusing documentation, anything is appreciated.

Ok, what this snipp does is allow you to save your Time settings, so that when you copyover/reboot
the time (day/hour/year) all stay the same. Useful for timed events. (I've even gone so far
as to make a seprate program that ONLY updates time, and the mud checks once a minuite for a time
change to make sure in-game time remains constant always, good for timed events... anyways..)


Ok, First things first, the database to hold this information..
You can change it, it doesnt matter... just update the code ;)

----------

CREATE TABLE `limbo_time` (
  `place_holder` varchar(40) NOT NULL,
  `hour` int(11) NOT NULL,
  `day` int(11) NOT NULL,
  `month` int(11) NOT NULL,
  `year` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

-- 
-- Dumping data for table `limbo_time`
-- 

INSERT INTO `limbo_time` (`place_holder`, `hour`, `day`, `month`, `year`) VALUES ('prime', 0, 11, 2, 1562);

----------

place_holder is merely for finding the entry faster, or slower? hmm, anyways, its always
going to be 'prime' as its value. I'll keep it myself simply because I may have multiple worlds?
Who the hell knows with my mud, 8 years in development, and it still not open ;)
the rest of the entries are just ints, nothing special.


/*
 * Installation..
 */
Now on to the code.., you'll need to connect to the database..

Make a mysql.h file and put in..

----------
#include <mysql/mysql.h>

MYSQL               mysqlconn;
MYSQL_RES           *result;
MYSQL_ROW           row;

extern int          sql_working;

/* declarations */
#define MY_SERVER   "localhost"
#define MY_USER     "YOUR USER"
#define MY_PWD      "YOUR PASSWORD"
#define MY_DB       "YOUR DATABASE"
#define MY_SOCK     NULL
#define MY_PORT     3306
#define MY_FLAG     0

#define bool        int
#define TRUE        1
#define FALSE       0

void load_mysql     (void);
int MySQLQuery      (char * query);
bool open_db        (void);
void close_db       (void);
---------

Now you need the handler functions..
Make a mysql.c file and put..

(Any suggestions? Did I do this right? It seems to work..)

---------
/*
 * load_mysql()
 *
 * Opens the database, provides errors, etc
 */
void load_mysql(void)
{
    if (!open_db() )
    {
        logf("**** MYSQL ****: Could not open Database.\n");
        logf("**** MYSQL ****: Reason:\n%s\n", mysql_error(&mysqlconn));
        exit (1);
        return;
    }
    logf("**** MYSQL ****: Connection open and working.\n");
    sql_working = 1;
    return;
}

/*
 * MySQLQuery()
 *
 * Inserts a raw query to the SQL server
 * returns true if it works, false if it fails
 */
int MySQLQuery(char * query)
{
    if (mysql_real_query(&mysqlconn, query, strlen(query)))
    {
        logf("**** MYSQL ****: Error:\n%s\n", mysql_error(&mysqlconn));
        return 0;
    }
    mysql_free_result(result);
    return 1;
}
    
/*
 * open_db()
 *
 * Opens a connection to the SQL database
 */
bool open_db(void)
{
    if (!mysql_init(&mysqlconn))
    {
        logf("**** MYSQL ****: Unable to open database!\n");
        return FALSE;
    }
    if ((mysql_real_connect(&mysqlconn, MY_SERVER, MY_USER, MY_PWD, MY_DB, MY_PORT, MY_SOCK, MY_FLAG)) == NULL)
    {
        logf("**** MYSQL ****: open_db() failed during opening!\n");
        mysql_close(&mysqlconn);
        return FALSE;
    }
    return TRUE;
}

/*
 * close_db()
 *
 * Closes connection to the SQL server
 */
void close_db(void)
{
    logf("**** MYSQL ****: close_db(): closed\n");
    mysql_close(&mysqlconn);
    return;
}
----------

Now stick load_mysql(); into boot_db (db.c) somewhere, near the end..

also stick
#include "mysql.h"
at the top of db.c

Anywhere you shutdown/reboot/copyover, or in any way kill the mud, put

close_db(); to the end.

that means..

do_copyover()
do_reboot()
do_shutdown()

etc etc etc, its important ;)


Also in db.c, you'll need a way to load the time, right? Stick..

void load_time()
{ 
    char query[4096];
    MYSQL_RES *result;

    sprintf(query, "SELECT * FROM `limbo_time` WHERE CONVERT( `place_holder` USING utf8 ) = 'prime' LIMIT 1");
    MySQLQuery(query);

    result = mysql_store_result(&mysqlconn);

    while ((row = mysql_fetch_row(result)))
    {
        time_info.hour  = atoi(row[1]);
        time_info.day   = atoi(row[2]);
        time_info.month = atoi(row[3]);
        time_info.year  = atoi(row[4]);
    }

    mysql_free_result(result);
    return;
}

at the end of db.c and put load_time(); under load_mysql.c in boot_db();
Err, so it looks like..

	load_classes();
	load_mysql();
	load_time();

Now, goto update.c, were gonna make it so that once per day, it saves your time settings,
optionaly, you can always update it every hour.. but for this application, once a day is
just fine. At the top of update.c, put
#include "mysql.h"

In weather_update(), in case 24: (ie the day change) put..

            sprintf(query, "UPDATE `limbo_time` SET `hour` = '%d', `day` = '%d', `month` = '%d', `year` = '%d' WHERE CONVERT( `place_holder` USING utf8 ) = 'prime' LIMIT 1 ;",
                    time_info.hour,
                    time_info.day,
                    time_info.month,
                    time_info.year);
            MySQLQuery(query);