I'm working on an experimental project to modify SocketMUD 2.2 to use a MySQL database backend instead of external files. I've already gotten the most complex stuff done and conducted several successful database retrieval tests from within the MUD.
However, I've run into a brick wall on something that is probably insultingly simple, and yet after 6 straight hours of Googling, trial-and-error(ing), and banging my head against a wall, I am no closer to a solution than when I started! I'm widely considered an expert in PHP scripting and have some but fairly little experience with C/C++.
Here's what I'm trying to do: I'm working on a simple function that is passed a query string, queries the database, and returns the results in a multidimensional char array. In PHP, here is a conceptual equivalent of the type of variable I'm talking about: $fictional_results[$row_int][$column_int] = $col_value;
In PHP, I could have that working in a matter of seconds. But in C, just about anything I do returns a segmentation fault on runtime (not exactly the most helpful error message I might add). The codebase is written in C flat (i.e. not C++), and the string.h library does not seem compatible (if I try to #include <string.h> it's fine, but if I try to declare any variable as type string it returns a non-sensical parse error on compile).
Here are the relevant declarations in mud.h:
#include "/usr/include/mysql/mysql.h"
...
typedef struct mysql_xdata
{
char xrowdata[1024][1024][1024]; /* This is the variable I'm having trouble with. I've tried "char *xrowdata[1024][1024]", "char xrowdata[1024][1024]", etc, as well as just about every suggestion on Google.... Nothing works! All I want is string_variable[mysql_row_int][mysql_column_int]. */
int xnumrows;
int xnumcols;
char xmysqldebugbuf[1024]; /* Used for variable DEBUG. --XXXX */
} XMYSQL;
...
/*
* mysql.c
*/
XMYSQL *mysql_xquery( char xqueryx[1024] );
Now here's the function (in its current form, with a few things like passwords censored out) that performs the query in mysql.c:
XMYSQL *mysql_xquery( char xqueryx[1024] )
{
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "XXXX";
char *database = "XXXX";
int column = 0;
int sqlrow = 0;
int totalcols = 0;
XMYSQL *xmysql;
xmysql = malloc( sizeof( XMYSQL ) );
sprintf( xmysql->xmysqldebugbuf, "DEBUG SUCCESSFUL!" );
conn = mysql_init(NULL);
/* Connect to database */
if ( !mysql_real_connect( conn, server, user, password, database, 0, NULL, 0 ) )
{
fprintf( stderr, "%s\n", mysql_error( conn ) );
exit( 0 );
}
/* send SQL query */
if ( mysql_query( conn, xqueryx ) )
{
fprintf( stderr, "%s\n", mysql_error( conn ) );
exit( 0 );
}
res = mysql_use_result( conn );
/* output fields of each row */
while ( ( row = mysql_fetch_row( res ) ) != NULL )
{
column = 0;
sqlrow++;
while ( column < res->field_count )
{
column++;
totalcols++;
sprintf( xmysql->xrowdata[sqlrow][column], "%s", row[column - 1] ); /* !! ATTENTION !! -- This is the line that is causing the segmentation fault during runtime. If I comment it out, everything works fine, and all the other variables retain their values perfectly, and I have also tested extensively to ensure it's not an error with the MySQL interaction itself. */
}
}
/* Release memory used to store results and close connection */
mysql_free_result( res );
mysql_close( conn );
xmysql->xnumrows = sqlrow;
xmysql->xnumcols = column;
return xmysql;
}
Currently, I'm calling the function from socket.c just as a test debug, essentially to display the output of a test query whenever I connect and it displays the greeting stuff. The code for that isn't relevant to this I don't think, but I can include it anyway if you think it'll help.
I feel really frustrated being held in place by such a basic problem. I do know how C char arrays work (i.e. only stores one char for each key), but I can't seem to wrap my brain around how to go from that to creating a multi-dimensional string array that does what I'm trying to do.
Whew! Any ideas? I really appreciate any help you can provide. =)
--TT
|