Renegade Public Forums
C&C: Renegade --> Dying since 2003™, resurrected in 2024!
Home » Renegade Discussions » Mod Forum » MySQL with SSGM?
MySQL with SSGM? [message #432883] Fri, 16 July 2010 17:48 Go to next message
TechnoBulldog is currently offline  TechnoBulldog
Messages: 15
Registered: May 2010
Location: Texas
Karma: 0
Recruit
It's sad that the only reason I'm posting is because I have a problem.

I'm new to making c++ SSGM plugins and MySQL with c++. I'm trying to make a simple "!postnote" command that will add a player's name and message to a table in the database (I know someone is going to say "Why don't you use fstream or another io function?" It's because I want to experiment with databases). At the moment, I've been able to make it connect, but whenever it tries to execute a query, the server crashes. It doesn't seem to have any trouble with connecting to MySQL, I mean, if I use the correct username and password, it works fine, and when it's the incorrect username and password, it tells me there was an error. I only seem to have a problem when I try to execute a query, at which point, the server crashes without adding anything to the database. I also get 7 warnings when I compile it, all coming from "cppconn/exception.h". I made a simple console application using the same stuff and it worked fine.

I suppose my first question would be, is it possible to have MySQL in an SSGM plugin?

Second would be, is there an obvious reason it is crashing my server?

Toggle Spoiler


If I've made a stupid mistake somewhere, please point it out. If you think you might be able to help, but I left something out, let me know and I'll try to post the info. This is bugging me a lot haha. I also have a bad feeling this is the wrong place to post this...


Cloud-Zone Gaming. Check it out.

^^ Awesome right?
Re: MySQL with SSGM? [message #432893 is a reply to message #432883] Fri, 16 July 2010 21:47 Go to previous messageGo to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
TechnoBulldog wrote on Fri, 16 July 2010 20:48

It's sad that the only reason I'm posting is because I have a problem.

I'm new to making c++ SSGM plugins and MySQL with c++. I'm trying to make a simple "!postnote" command that will add a player's name and message to a table in the database (I know someone is going to say "Why don't you use fstream or another io function?" It's because I want to experiment with databases). At the moment, I've been able to make it connect, but whenever it tries to execute a query, the server crashes. It doesn't seem to have any trouble with connecting to MySQL, I mean, if I use the correct username and password, it works fine, and when it's the incorrect username and password, it tells me there was an error. I only seem to have a problem when I try to execute a query, at which point, the server crashes without adding anything to the database. I also get 7 warnings when I compile it, all coming from "cppconn/exception.h". I made a simple console application using the same stuff and it worked fine.

I suppose my first question would be, is it possible to have MySQL in an SSGM plugin?

Second would be, is there an obvious reason it is crashing my server?

Toggle Spoiler


If I've made a stupid mistake somewhere, please point it out. If you think you might be able to help, but I left something out, let me know and I'll try to post the info. This is bugging me a lot haha. I also have a bad feeling this is the wrong place to post this...


Welcome to the forums. =]
The FDS can interact with databases, this shouldn't be a problem.
Infact, someone called "jnz" (an awesome chap) almost finished a bot that interacted with a database (before he decided to halt development), you can see the bot's code in this release, here: http://www.littlebigsite.co.uk/Misc/Files/Projects.rar

Sadly, I do not know much about database interaction and MySQL, so I cannot help much to be honest, although I would if I could...

If your console application worked fine, and it is connecting to the database fine, then I think we can assume it's the query itself that's causing an error?

I would be keen to see the console applications query...
Perhaps you could try a test query that does not rely on the player GameObject's, but just a normal string?

I am thinking there might be a possible cause in the below code, because you're using a std string, and it should be converted to a c string.

std::string query = StrFormat("INSERT INTO messages(name, message) VALUES('%s', '%s')", pName, Text[0]);


Should be:
std::string query = StrFormat("INSERT INTO messages(name, message) VALUES('%s', '%s')", pName.c_str(), Text[0]/*may also need to be concerted to a c string?*/);


I'm glad you did post this question because it's an excellent thread!



[Updated on: Fri, 16 July 2010 21:54]

Report message to a moderator

Re: MySQL with SSGM? [message #432908 is a reply to message #432883] Sat, 17 July 2010 01:44 Go to previous messageGo to next message
danpaul88 is currently offline  danpaul88
Messages: 5795
Registered: June 2004
Location: England
Karma: 0
General (5 Stars)
Yes, I believe StrFormat would expect C strings for %s parameters and would crash if you gave it std::string instead. You should also convert Text[0] to a C string.

Internally StrFormat simply uses sprintf (or an equivalent of) and expects a character array for the relevant parameter to a %s tag and passing anything other than a char array (c string) would cause a crash.


http://steamsignature.com/card/1/76561197975867233.png

[Updated on: Sat, 17 July 2010 01:45]

Report message to a moderator

Re: MySQL with SSGM? [message #432912 is a reply to message #432908] Sat, 17 July 2010 02:23 Go to previous messageGo to next message
Hex is currently offline  Hex
Messages: 858
Registered: March 2004
Karma: 0
Colonel
Pull the sqlite stuff and stacker from jnz's code

goztow wrote on Tue, 11 May 2010 08:00

If we had to ban all who ever cheated or ever created a cheat (obj3cts and such) then I don't think there would be many members left here (sad fact).


reborn wrote on Fri, 29 January 2010 23:37

std is for pro's. Razz
Re: MySQL with SSGM? [message #432922 is a reply to message #432883] Sat, 17 July 2010 03:45 Go to previous messageGo to next message
Sladewill is currently offline  Sladewill
Messages: 291
Registered: January 2009
Location: United Kingdom
Karma: 0
Recruit

yes u can use C++ and mysql i actually infact have finished using the mysql C++ connetor to connect to a database, take a look at this artical http://lstigile.wordpress.com/2009/05/19/using-libmysqld-with-microsoft-visual-c -2008-express/

FT-Owners - Sladewill,Snazy2007,Willdy
http://FT-Gaming.com for more info...
Re: MySQL with SSGM? [message #432930 is a reply to message #432883] Sat, 17 July 2010 06:57 Go to previous messageGo to next message
TechnoBulldog is currently offline  TechnoBulldog
Messages: 15
Registered: May 2010
Location: Texas
Karma: 0
Recruit
Wow, thanks for the help! It's working now!

I'm also wondering if there is a mysql_real_escape_string function for c++ like there is for PHP. This would help a lot. Also, does anyone have a clue why I get 6 warnings?

Warnings:

Toggle Spoiler


Also, reborn, it was a very simple console application. This was the query in my test console application.

statement->execute("INSERT INTO cms_content(alias, body) VALUES('cplusplus', 'does it work?')");


And the queries I posted in my code (both of which now work):

std::string query = StrFormat("INSERT INTO messages(name, message) VALUES('%s', '%s')", pName, Text[0]);
			
// Will cause server to crash.
//statement->execute(query);

// Will cause server to crash.
//statement->executeQuery("INSERT INTO messages(name, message) VALUES('tester5', 'testing value')");


But really, thanks for the help everyone! I think this plugin might actually be useful when it's done.


Cloud-Zone Gaming. Check it out.

^^ Awesome right?
Re: MySQL with SSGM? [message #432950 is a reply to message #432930] Sat, 17 July 2010 11:08 Go to previous messageGo to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
I'm glad you've got it working, welldone!

Can I ask what it is you plan to do with this? What are you working on? Anything for rene that needs DB support sounds pretty interesting. What are your plans?

/* google this, para phrasing, but from daniweb:
mysql_real_escape_string is actually from the MySQL C API, rather than the developers of PHP. The PHP developers created addslashes().


char * S = new char[strlen(m_headerSubject.c_str())*3 +1];
mysql_real_escape_string(conn, S, m_headerSubject.c_str(), m_headerSubject.length());
m_headerSubject = m_headerSubject.assign(S);
*/



Re: MySQL with SSGM? [message #432952 is a reply to message #432950] Sat, 17 July 2010 11:26 Go to previous messageGo to next message
TechnoBulldog is currently offline  TechnoBulldog
Messages: 15
Registered: May 2010
Location: Texas
Karma: 0
Recruit
reborn wrote on Sat, 17 July 2010 13:08

I'm glad you've got it working, welldone!

Can I ask what it is you plan to do with this? What are you working on? Anything for rene that needs DB support sounds pretty interesting. What are your plans?

/* google this, para phrasing, but from daniweb:
mysql_real_escape_string is actually from the MySQL C API, rather than the developers of PHP. The PHP developers created addslashes().


char * S = new char[strlen(m_headerSubject.c_str())*3 +1];
mysql_real_escape_string(conn, S, m_headerSubject.c_str(), m_headerSubject.length());
m_headerSubject = m_headerSubject.assign(S);
*/


I did read something like that, but I can't find the function. I'm guessing it's in a file I haven't included.

Originally, I had the idea to use a database because a friend of mine, Sonic (Sonic2876) said he was planning on opening up a few servers, his main focus being a build server. He said he was having issues making "!load" and "!save" in Lua, and I thought that I might be able to do it in c++. However, I've never gone very deep in c++ (my learning level is somewhere around intermediate) and I never got into fstream or any of those functions. I have recently been experimenting with MySQL and php, though, and the first thing I thought of was storing the objects in a database. I figured it would be cleaner and more efficient.

However, I am nowhere near ready to make a build server, so I decided to experiment around first. The idea for this plugin came from Cloud, my main server and one I moderate on. DarkOrbit has a lua script for "!postnote" that I think writes to a file, but I'm not entirely sure. I thought that if you wrote it to a database, a simple php script could let you view all of the notes from in game players and clear it relatively easily. If you write it to a file, you have to manually connect the server and find the file, which sounds like a hassle. At the moment, I just need to find a way to prevent injection attacks (which work easier than I expected) and write the php page. Otherwise, I think this simple but useful plugin will be done.


Cloud-Zone Gaming. Check it out.

^^ Awesome right?
Re: MySQL with SSGM? [message #432963 is a reply to message #432883] Sat, 17 July 2010 13:00 Go to previous messageGo to next message
danpaul88 is currently offline  danpaul88
Messages: 5795
Registered: June 2004
Location: England
Karma: 0
General (5 Stars)
mysql_real_escape_string is in the MySQL library, NOT the c++ connector.... to be honest I am not sure why there IS a c++ connector when you can use the mysql library directly in c++ anyway...


The function you want is defined in the MySQL API which is installed alongside MySQL if you select the installation option to do so. You will also need to link to libmysql.dll, which is included with the API.

However I doubt that you will have much success mixing and matching the c++ connector with the raw API functions and you may have to convert your existing code to use the API directly (which, to be honest, is more efficient anyway).


http://steamsignature.com/card/1/76561197975867233.png
Re: MySQL with SSGM? [message #432970 is a reply to message #432963] Sat, 17 July 2010 13:25 Go to previous messageGo to next message
TechnoBulldog is currently offline  TechnoBulldog
Messages: 15
Registered: May 2010
Location: Texas
Karma: 0
Recruit
danpaul88 wrote on Sat, 17 July 2010 15:00

mysql_real_escape_string is in the MySQL library, NOT the c++ connector.... to be honest I am not sure why there IS a c++ connector when you can use the mysql library directly in c++ anyway...


The function you want is defined in the MySQL API which is installed alongside MySQL if you select the installation option to do so. You will also need to link to libmysql.dll, which is included with the API.

However I doubt that you will have much success mixing and matching the c++ connector with the raw API functions and you may have to convert your existing code to use the API directly (which, to be honest, is more efficient anyway).

I have the C API, but I'm not sure how to use it really. Is there a specific header file I need to include?


Cloud-Zone Gaming. Check it out.

^^ Awesome right?
Re: MySQL with SSGM? [message #432974 is a reply to message #432883] Sat, 17 July 2010 13:51 Go to previous messageGo to next message
danpaul88 is currently offline  danpaul88
Messages: 5795
Registered: June 2004
Location: England
Karma: 0
General (5 Stars)
mysql.h should be the only header you need to include to use the library, just make sure to also add libmysql.dll to the linker input options.

http://steamsignature.com/card/1/76561197975867233.png
Re: MySQL with SSGM? [message #432977 is a reply to message #432974] Sat, 17 July 2010 14:01 Go to previous messageGo to next message
TechnoBulldog is currently offline  TechnoBulldog
Messages: 15
Registered: May 2010
Location: Texas
Karma: 0
Recruit
When I press build after including mysql.h, it has six errors in mysql_com.h, as well as the other six warnings.

Toggle Spoiler


Cloud-Zone Gaming. Check it out.

^^ Awesome right?
Re: MySQL with SSGM? [message #433034 is a reply to message #432883] Sun, 18 July 2010 03:15 Go to previous messageGo to next message
Sladewill is currently offline  Sladewill
Messages: 291
Registered: January 2009
Location: United Kingdom
Karma: 0
Recruit

#include "windows.h"

using namespace::std;


They are what fixed it for me yesterday.


FT-Owners - Sladewill,Snazy2007,Willdy
http://FT-Gaming.com for more info...
Re: MySQL with SSGM? [message #433043 is a reply to message #433034] Sun, 18 July 2010 06:58 Go to previous messageGo to next message
TechnoBulldog is currently offline  TechnoBulldog
Messages: 15
Registered: May 2010
Location: Texas
Karma: 0
Recruit
Sladewill wrote on Sun, 18 July 2010 05:15

#include "windows.h"

using namespace::std;


They are what fixed it for me yesterday.

Oh, yeah, that fixes it, I think. I think WIN32_LEAN_AND_MEAN was messing it up, as well as the order it was in. I can access the function now, but I'm trying to find a way to use the c++ connector instead of an open C connection. I have a feeling that's impossible (lol) but I'm going to keep looking for a way.

Again, thanks for the help so far everyone!

EDIT: I've found out that if you use mysql_escape_string (no real) it doesn't require an open connection, but it also doesn't use the current character set, or whatever that is.

http://dev.mysql.com/doc/refman/5.1/en/mysql-escape-string.html
Quote:


You should use mysql_real_escape_string() instead!

This function is identical to mysql_real_escape_string() except that mysql_real_escape_string() takes a connection handler as its first argument and escapes the string according to the current character set. mysql_escape_string() does not take a connection argument and does not respect the current character set.



Cloud-Zone Gaming. Check it out.

^^ Awesome right?

[Updated on: Sun, 18 July 2010 07:36]

Report message to a moderator

Re: MySQL with SSGM? [message #433045 is a reply to message #432883] Sun, 18 July 2010 08:42 Go to previous messageGo to next message
danpaul88 is currently offline  danpaul88
Messages: 5795
Registered: June 2004
Location: England
Karma: 0
General (5 Stars)
Why are you against using the connection and query functions in the MySQL API rather than the ones in the C++ connector? Using them directly would be more efficient in the long run, since the C++ connector is doing that internally anyway but adding an extra level between your program and the API.


MySQL* db_connection = mysql_init(NULL); 
mysql_real_connect ( db_connection, "localhost", "username", "password", "schema", NULL, NULL, NULL );



Connecting to a MySQL database using the API is that simple. Anyway, obviously it's up to you to decide which way you prefer but I am still a bit confused why anyone would use the c++ connector when the API works in c++ anyway...


http://steamsignature.com/card/1/76561197975867233.png

[Updated on: Sun, 18 July 2010 08:42]

Report message to a moderator

Re: MySQL with SSGM? [message #433051 is a reply to message #433045] Sun, 18 July 2010 10:41 Go to previous messageGo to next message
TechnoBulldog is currently offline  TechnoBulldog
Messages: 15
Registered: May 2010
Location: Texas
Karma: 0
Recruit
danpaul88 wrote on Sun, 18 July 2010 10:42

Why are you against using the connection and query functions in the MySQL API rather than the ones in the C++ connector? Using them directly would be more efficient in the long run, since the C++ connector is doing that internally anyway but adding an extra level between your program and the API.


MySQL* db_connection = mysql_init(NULL); 
mysql_real_connect ( db_connection, "localhost", "username", "password", "schema", NULL, NULL, NULL );



Connecting to a MySQL database using the API is that simple. Anyway, obviously it's up to you to decide which way you prefer but I am still a bit confused why anyone would use the c++ connector when the API works in c++ anyway...

Mostly because I'm stubborn and am not used to the C API. However, I'll look into it if it's as simple as you say lol. I'll look into it later, though, I'm a bit busy right now.


Cloud-Zone Gaming. Check it out.

^^ Awesome right?
Re: MySQL with SSGM? [message #433053 is a reply to message #432883] Sun, 18 July 2010 12:20 Go to previous messageGo to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
Is the plugin itself creating the database and tables and such, or are you manually creating that first, then allowing the plugin to use it?

Is the plugin multi-threaded? Does it need to be?

I don't know much about databases at all, really, let alone securing it. Would you need to make it so secure if the database was being access locally on the machine, and was not set-up for remote access to the DB?




Re: MySQL with SSGM? [message #433057 is a reply to message #433053] Sun, 18 July 2010 12:52 Go to previous messageGo to next message
TechnoBulldog is currently offline  TechnoBulldog
Messages: 15
Registered: May 2010
Location: Texas
Karma: 0
Recruit
reborn wrote on Sun, 18 July 2010 14:20

Is the plugin itself creating the database and tables and such, or are you manually creating that first, then allowing the plugin to use it?

Is the plugin multi-threaded? Does it need to be?

I don't know much about databases at all, really, let alone securing it. Would you need to make it so secure if the database was being access locally on the machine, and was not set-up for remote access to the DB?



No the plugin itself does not create the databases or tables. I'm not really sure what you mean by multi-threaded, but my guess is that it means to break it down into parts as not to cause lag? If that's the case, no, it doesn't, and I don't think it needs to. It's not a command that will be used a lot, the only thing it does is add something to a small table in a database.

I was kinda planning on it only working on one machine, but there is a username and password needed to connect. The only thing I'm worried about is injections, which I don't know a lot about, but I believe this is what they are.

Say you type !postnote <message> and the message gets sent to the database. The query would look something like:
("INSERT INTO messages(name, message) VALUES('%s', '%s')", pName.c_str(), M.c_str())

If someone typed !postnote hello the message stored in the database would say "hello". However, if someone was to type something like "!postnote '); DROP renegade;" the literal translation of the query would be:
("INSERT INTO messages(name, message) VALUES('hacker', ''); DROP renegade; ')"

Although there would be an error with that code, the basic principal of that is that they could control your database. If the above query didn't have an error, "DROP renegade" would have deleted the entire database, and that obviously wouldn't be good. If I can secure the database to only work with this account on 127.0.0.1 and escape the string (like add a \ before the quotation marks and whatnot) that eliminates two of the main ways they can get in.

Does that explain anything?


Cloud-Zone Gaming. Check it out.

^^ Awesome right?
Re: MySQL with SSGM? [message #433060 is a reply to message #433057] Sun, 18 July 2010 13:02 Go to previous messageGo to next message
reborn is currently offline  reborn
Messages: 3231
Registered: September 2004
Location: uk - london
Karma: 0
General (3 Stars)
Ah, I get it, thank you.



Re: MySQL with SSGM? [message #433061 is a reply to message #432883] Sun, 18 July 2010 13:24 Go to previous messageGo to next message
Sladewill is currently offline  Sladewill
Messages: 291
Registered: January 2009
Location: United Kingdom
Karma: 0
Recruit

what u can do is only allow alpha numerical digits to be used in a query which is a handy function we've used to prevent users from posting horrid names into saves which causes a server crash.

FT-Owners - Sladewill,Snazy2007,Willdy
http://FT-Gaming.com for more info...
Re: MySQL with SSGM? [message #433120 is a reply to message #433061] Mon, 19 July 2010 06:42 Go to previous message
TechnoBulldog is currently offline  TechnoBulldog
Messages: 15
Registered: May 2010
Location: Texas
Karma: 0
Recruit
Couldn't mysql_real_escape_string work? Then if I run it back out through php, I can just use strip stripslashes I think.

Also, would it be more effecient to reconnect every time someone types !postnote or to have an open connection and only send a query when someone types it?


Cloud-Zone Gaming. Check it out.

^^ Awesome right?

[Updated on: Mon, 19 July 2010 07:00]

Report message to a moderator

Previous Topic: Help with sockets
Next Topic: Human Anims
Goto Forum:
  


Current Time: Mon Nov 18 11:04:39 MST 2024

Total time taken to generate the page: 0.01156 seconds