Connecting to MySQL

I'm creating a console based game, with account login to a MySQL database(at least I'd like to to it)

so what would be a simple way to achieve that?
I've googled for it but most of them refer either to local DB or are based on Win32...

Practicaly what I need is a online connection to DB, as simple and raw as possible, and is there any way to handle SQL like in PHP?
Connecting to a local MYSQL database is the same as connecting to a remote database.
PHP is one the languages that have binding to MYSQL - go the MYSQL website for more information ( I wouldn't be surprised if direct support for MYSQL was already built into PHP).

Last edited on
actualy i know how to do that in PHP, just looking for a c++ way...
Well I don't know how good it is, but they did have a MySQL++ that you could use for C++/MySQL connections. Though I remember it was a pain to build. I don't know if anything easier has been made.
well now i downloaded the libs from mysql, trying to compile this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
extern "C"
{
#include "C:\sql\include\mysql.h"
}
using namespace std;

MYSQL *connection,mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;

void main()
{
	mysql_init(&mysql);
	connection = mysql_real_connect(&mysql,"host","user","password","database",0,0,0);
	if(connection==NULL)
	{
		cout<<mysql_error(&mysql)<<endl;
	}
	else
	{
		cout<<"CONNECTED!"<<endl;
	}
}

this is the error list:
Error	1	error C2146: syntax error : missing ';' before identifier 'fd'	c:\sql\include\mysql_com.h	291	1	sqltest
Error	2	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\sql\include\mysql_com.h	291	1	sqltest
Error	3	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\sql\include\mysql_com.h	291	1	sqltest
Error	4	error C2065: 'SOCKET' : undeclared identifier	c:\sql\include\mysql_com.h	470	1	sqltest
Error	5	error C2146: syntax error : missing ')' before identifier 's'	c:\sql\include\mysql_com.h	470	1	sqltest
Error	6	error C2059: syntax error : ')'	c:\sql\include\mysql_com.h	471	1	sqltest


doesn't matter if i delete the extern "C"{}, everything stays the same...
1
2
3
//Comment - Need to include windows.h BEFORE mysql.h 
#include <windows.h>
#include "C:\sql\include\mysql.h" 



ps ---- void main() ????????????????????????
Last edited on
I don't really understand why they made a C++ wrapper for the C API (MySQL++ = http://www.tangentsoft.net/mysql++/ ) because shouldn't the C API work in C++?
The mysql C api will work in C++ - I suppose someone just wanted to do a c++ wrapper.
ok thx for info, i'll try it as soon as i get to a pc with compiler. Ps. what's wrong with void main? it doesn't need to return anything :p
If your compiler is allowing void main it means you are using a compiler that holds to the outdated standard.
well I'm using Visual studio 2010, don't think that it's outdated...

oh and trying to include windows.h i got this:
1
2
3
4
Error	1	error LNK2019: unresolved external symbol _mysql_error@4 referenced in function _main	C:\Users\Žan\Documents\Visual Studio 2010\Projects\sqltest\sqltest\main.obj	sqltest
Error	2	error LNK2019: unresolved external symbol _mysql_real_connect@32 referenced in function _main	C:\Users\Žan\Documents\Visual Studio 2010\Projects\sqltest\sqltest\main.obj	sqltest
Error	3	error LNK2019: unresolved external symbol _mysql_init@4 referenced in function _main	C:\Users\Žan\Documents\Visual Studio 2010\Projects\sqltest\sqltest\main.obj	sqltest
Error	4	error LNK1120: 3 unresolved externals	C:\Users\Žan\Documents\Visual Studio 2010\Projects\sqltest\Debug\sqltest.exe	sqltest

EDIT: forgot to link, nevermind that :p
Last edited on
Your original errors have gone - I was expecting this to happen - You now have
linker errors - you need to link in the mysql.lib library to the project so
that the linker can link in these mysql functions.

Do you know much about how to put a c++ program together???
well I'm using Visual studio 2010, don't think that it's outdated...

No, but Microsoft has never adhered to the standard. I'd recommend changing to a compiler that is more strict to the standard (main reason I've not touched any MSVC versions even though I have them installed on my PC). The standard (since the 98 revision I believe) has said that main() must return a value (ie int main() so allowing void main is against the standard. Though it does make me laugh as it says explicitly passing return 0; at the end is optional now.
Last edited on by closed account z6A9GNh0
why's void main actualy so bad?
Firstly, it's just plain wrong. The C++ standard is the definition of what is and is not C++, and void main() is not C++.

Secondly, when your program runs, it does not start with main. It starts with something else. That something else has some housekeeping to do, and then it calls the main function. It expects that the main function will return an int. There will be memory set aside to hold that returned value. If the main function doesn't actually return a value, you're asking for trouble.

More here: http://www.gidnetwork.com/b-66.html
I use GNU under Ubuntu and MinG under Vista. I did this under Ubuntu real fast last night:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <mysql.h>
#include <cstdio>

int main()
{
	MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char *server = "server";
	char *user = "user";
	char *password = "password";  // got tot keep my data secret
	char *database = "cpp_test";
	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));
		return -1;
	}
	
	// send SQL query
	if(mysql_query(conn, "select * from cpp_testTAB"))
	{
		fprintf(stderr, "%s\n", mysql_error(conn));
		return -1;
	}
	
	res = mysql_use_result(conn);
	
	// output table name
	printf("MySQL Tables in mysql database:\n");
	while ((row = mysql_fetch_row(res)) != NULL)
	{
		printf("%s %s %s %s\n", row[0], row[1], row[2], row[3]);
	}
	
	// close connection
	mysql_free_result(res);
	mysql_close(conn);
	
	return 0;
}

Output is:

MySQL Tables in mysql database:
1 John Bartholomew Pippin
2 Thomas Tiberius Tabernathy
3 Delta Echo Gamma
Topic archived. No new replies allowed.