Read MySQL database table rows

Hello. So I have this C++ 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
void mysql_do_ds_awards()
{
#ifdef USE_MYSQL
	char sql_cmd[5000];
	MYSQL_RES *res;
	MYSQL_ROW row;
	unsigned int num_fields;
	unsigned int i;
	char *server = "localhost";
	char *db_user = MYSQL_USER;
	char *db_password = MYSQL_PASSWORD;
	char *database = "game_pay_db";
	MYSQL *my_sql_here_conn;

	//make connection
	while(mysql_mutex)
		uni_pause(100);
	mysql_mutex = 1;

	my_sql_here_conn = mysql_init(NULL);

	if(!my_sql_here_conn)
	{
		printf("mysql: cannot init mysql for ds awards\n");
		mysql_mutex = 0;
		return;
	}

	if(mysql_real_connect(my_sql_here_conn, server, db_user, db_password, database, 0, NULL, CLIENT_FOUND_ROWS))
	{
		printf("mysql: connection established for ds awards\n");
	}
	else
	{
		printf("mysql: cannot connect ds awards: %s\n", mysql_error(my_sql_here_conn));
		mysql_close(my_sql_here_conn);
		mysql_mutex = 0;
		return;
	}

	sprintf(sql_cmd, "SELECT * FROM payout_entry WHERE was_paid = 2");
	if(mysql_query(my_sql_here_conn, sql_cmd))
	{
		printf("mysql: selection error: %s\n", mysql_error(my_sql_here_conn));
		mysql_close(my_sql_here_conn);
		mysql_mutex = 0;
		return;
	}


	res=mysql_store_result(my_sql_here_conn); /* Download result from server */
	num_fields = mysql_num_fields(res);
	while ((row = mysql_fetch_row(res)))
	{
		unsigned long *lengths;
		int i;
		lengths = mysql_fetch_lengths(res);

		char username[2][500];
		int ds_tobe[2], tabID;

		tabID = atoi(row[0]);
		strcpy(username[0], row[1]);
		ds_tobe[0] = atoi(row[2]);
		strcpy(username[1], row[3]);
		ds_tobe[1] = atoi(row[4]);

		award_user_ds(username[0], ds_tobe[0]);
		award_user_ds(username[1], ds_tobe[1]);

		//mysql_query("UPDATE payout_entry SET was_paid='1' WHERE tabID='" . $the_id . "'");
		sprintf(sql_cmd, "UPDATE payout_entry SET was_paid='1' WHERE tabID='%d'", tabID);
		if(mysql_query(my_sql_here_conn, sql_cmd))
		{
			printf("mysql: pay marking error: %s\n", mysql_error(my_sql_here_conn));
			continue;
		}
	}

	printf("mysql: connection closed for ds awards\n");

	mysql_free_result(res); /* Release memory used to store results. */
	mysql_close(my_sql_here_conn);
	mysql_mutex = 0;
#endif
}

void mysql_do_mfc_awards()
{
#ifdef USE_MYSQL
	char sql_cmd[5000];
	MYSQL_RES *res;
	MYSQL_ROW row;
	unsigned int num_fields;
	unsigned int i;
	char *server = "localhost";
	char *db_user = MYSQL_USER;
	char *db_password = MYSQL_PASSWORD;
	char *database = "game_affiliate_db";
	MYSQL *my_sql_here_conn;

	//make connection
	while(mysql_mutex)
		uni_pause(100);
	mysql_mutex = 1;

	my_sql_here_conn = mysql_init(NULL);

	if(!my_sql_here_conn)
	{
		printf("mysql: cannot init mysql for mfc awards\n");
		mysql_mutex = 0;
		return;
	}

	if(mysql_real_connect(my_sql_here_conn, server, db_user, db_password, database, 0, NULL, CLIENT_FOUND_ROWS))
	{
		printf("mysql: connection established for mfc awards\n");
	}
	else
	{
		printf("mysql: cannot connect mfc awards: %s\n", mysql_error(my_sql_here_conn));
		mysql_close(my_sql_here_conn);
		mysql_mutex = 0;
		return;
	}

	sprintf(sql_cmd, "SELECT * FROM affiliate_user WHERE aff_exp > 0");
	if(mysql_query(my_sql_here_conn, sql_cmd))
	{
		printf("mysql: selection error: %s\n", mysql_error(my_sql_here_conn));
		mysql_close(my_sql_here_conn);
		mysql_mutex = 0;
		return;
	}


	res=mysql_store_result(my_sql_here_conn); /* Download result from server */
	num_fields = mysql_num_fields(res);
	while ((row = mysql_fetch_row(res)))
	{
		unsigned long *lengths;
		int i;
		lengths = mysql_fetch_lengths(res);

		char their_username[500];
		int exp_tobe, money_tobe, tabID;

		tabID = atoi(row[0]);
		strcpy(their_username, row[4]);
		exp_tobe = atoi(row[5]);
		money_tobe = atoi(row[6]);

		user_award_mfc(their_username, exp_tobe, money_tobe);

		//mysql_query("UPDATE affiliate_user SET aff_exp='0', aff_money='0'  WHERE tabID='" . $the_id . "'");
		sprintf(sql_cmd, "UPDATE affiliate_user SET aff_exp='0', aff_money='0'  WHERE tabID='%d'", tabID);
		if(mysql_query(my_sql_here_conn, sql_cmd))
		{
			printf("mysql: aff clearing error: %s\n", mysql_error(my_sql_here_conn));
			continue;
		}
	}

	printf("mysql: connection closed for mfc awards\n");

	mysql_free_result(res); /* Release memory used to store results. */
	mysql_close(my_sql_here_conn);
	mysql_mutex = 0;
#endif
}


I already have made the two MySQL databases game_pay_db and game_affiliate_db.
Now I don't really know MySQL and I don't know what tables I have to make inside these databases in order to make this code work.
When I make a table "payout_entry" in the database, it shows me a bunch of rows to fill in name, type, length, etc ... I think the C++ code shows exactly what tables and rows have to be created in the database, I just don't completely understand it, so...
Can I get a little help here, please ? Thanks.
Bump~ Anyone ??
Take a look at this:

http://www.w3schools.com/sql/sql_create_table.asp

You can and probably should create the tables with your program. Ignore the error when they are already created.


I would not recommend having two different database you need to connect to. Better just one with two tables.
Topic archived. No new replies allowed.