Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » C++, PHP and MySQL, or just C++ and MySQL?.

This thread is locked; no one can reply to it. rss feed Print
C++, PHP and MySQL, or just C++ and MySQL?.
AMCerasoli
Member #11,955
May 2010
avatar

Hi there, this is just a comment.

I would like to know what kind of approach would you prefer to use in this situations.

I was using PHP to modify my own database (just a simple file) but now I want to use MySQL. So... I was using libCurl to send the request using POST fields etc.

Now, there is MySQL++, which allows me to send and retrieve info, and I wouldn't need to use PHP or libCurl.

So what of these approach would you use?

  1. Still using libCurl and PHP to learn more about: libCurl, PHP and MySQL

  2. Or just use MySQL++ and forget about: libCurl and PHP?

Personally I would use number 1, because I want to learn more about PHP, but using it for what I see, using PHP as intermediary isn't not necessary so practically it's double work isn't?

van_houtte
Member #11,605
January 2010
avatar

PHP+MySQL is insanely easy to learn and use, i've used it to solve countless problems really quickly

-----
For assistance, please register and click on this link to PM a moderator

Sometimes you may have to send 3-4 messages

Mark Oates
Member #1,146
March 2001
avatar

Do 'em all.

There's also Boost.Asio for socket programming.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

bamccaig
Member #7,536
July 2006
avatar

I would say that it depends on the problem. I'm assuming C++ isn't optional then. It otherwise depends on how serious the project is and how much you want to learn. I would say that using PHP as an intermediary you would learn more about libcurl than about PHP. :) PHP is really easy (if ugly) so it doesn't take much to learn. I imagine the only annoying part about it would be handling errors gracefully. Then again, it does seem silly to require a Web server unless it's actually useful for something. :) And learning to access MySQL directly from C or C++ is also very valuable knowledge.

Oscar Giner
Member #2,207
April 2002
avatar

2 implies that you need to allow external connections to the database, which in general is not a good idea for security reasons. If you're using a web hosting service (specially if it's free) external access to the database is probably not even allowed.

Option 2 also forces you to GPL your program (unless you buy a commercial MySQL license), if that matters to you.

You can say that option 1 is overkill: having a full web server just for a simple application, though it's easier to implement. There's an option 3, which would be writing your own server software.

AMCerasoli
Member #11,955
May 2010
avatar

PHP+MySQL is insanely easy to learn and use, i've used it to solve countless problems really quickly

Yhea I have been reading MySQL and it's so easy. I have a simple file which consist in this:

name
time
name
time

Like...

Jhone
654
Carl
45
Nigga
789

and it's a really pain to read it from my program, and I haven't done the part where I modify the names and times depending in which is higher using PHP... With MySQL it's much more simple.

after retrieving the file look what I'm doing:

#SelectExpand
1 std::stringstream procesador(p_dato->string); 2 3 short aa=0; 4 short aaa=0; 5 6 for(int a=1;a<31;a++){ 7 8 if(a & 1){ 9 10 char temporal[50]; 11 12 procesador.getline(temporal, 50); 13 14 al_ustr_free(p_dato->nombres[aa]); 15 16 p_dato->nombres[aa] = al_ustr_new(temporal); 17 18 // Se elimina el caracter /r ya que getline solo elimina /n, y windows usa ambos. 19 if(al_ustr_has_suffix_cstr(p_dato->nombres[aa], "\r")) 20 al_ustr_remove_chr(p_dato->nombres[aa], al_ustr_offset(p_dato->nombres[aa], -1)); 21 22 aa++; 23 } 24 else{ 25 26 char temporal[50]; 27 28 char min_T[30], seg_T[3]; 29 30 procesador.getline(temporal, 50); 31 32 short segundos_T = atoi(temporal); 33 34 short min = 0, seg = 0; 35 36 for(;segundos_T > 0; segundos_T--){ 37 38 seg++; 39 if(seg>59){ min++; seg=0; } 40 41 } 42 43 sprintf(min_T,"%02d",min); 44 sprintf(seg_T,"%02d",seg); 45 46 al_ustr_append_cstr(p_dato->tiempos[aaa], min_T); 47 al_ustr_append_cstr(p_dato->tiempos[aaa], ":"); 48 al_ustr_append_cstr(p_dato->tiempos[aaa], seg_T); 49 50 if(al_ustr_has_suffix_cstr(p_dato->tiempos[aaa], "\r")) 51 al_ustr_remove_chr(p_dato->tiempos[aaa], al_ustr_offset(p_dato->tiempos[aaa], -1)); 52 53 aaa++; 54 55 } 56 }

It's really a mess... Since I'm storing the time like: "356 seconds" then I need to convert them to minutes and seconds... I think for all the MySQL is the answer.

bamccaig said:

I would say that it depends on the problem. I'm assuming C++ isn't optional then.

It's just for my game

Option 2 also forces you to GPL your program (unless you buy a commercial MySQL license), if that matters to you.

Wow, that is really a good point.

Basically I want to add some extra functions to my game, there is already two modes;

Winer Mode: The game takes the best time from the list (which is taken from the server) and start the game in countdown, when the time finish, that means that you're not going to appear first in the list, so it's pointless to still playing.

Normal Mode: The game uses a normal clock taking the time you took to finish the game (answering all the question) and put it on the list if you're in the first 15 best times.

But now I want to add an extra mode, and would be playing with online questions, basically everybody is able to create its own database and connect to it and play with the questions that are registered to that database.

So suppose that someone create the "Hallowing" database and start adding questions to it, if someone wants to connect to that database just introduce the name of that database and connects...

So yhea for all that, I will need MySQL for sure... But using PHP as intermediary would take a little bit of more work but I think it's worth it.

Look what I did! ;D Now my window have little buttons! Yay!

video

Tobias Dammers
Member #2,604
August 2002
avatar

If the goal is to maintain a highscore list, then MySQL is way too large. Have you looked into SQLite? It's perfect for the job - you get one file containing the entire database, and the entire database engine gets linked into your program, so you don't have to make the user install a database server.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Jonatan Hedborg
Member #4,886
July 2004
avatar

If this is for an online high-score list, I would go with PHP + mysql. You should probably never expose a database server to the general population ;)

AMCerasoli
Member #11,955
May 2010
avatar

If the goal is to maintain a highscore list, then MySQL is way too large. Have you looked into SQLite?

Yes, the problem is that my web hosting doesn't support it.

If this is for an online high-score list, I would go with PHP + mysql.

Yhea me to, I'm going to use MySQL + PHP + libCurl. Learning libCurl allows me to get experience in HTTP request while PHP + MySQL give me experience for webs too, you konw, using HTML/JavaScript... instead of C++.

2 implies that you need to allow external connections to the database, which in general is not a good idea for security reasons.

You should probably never expose a database server to the general population

I didn't know those security problems, that is a big problem too...

Tobias Dammers
Member #2,604
August 2002
avatar

Yes, the problem is that my web hosting doesn't support it.

Oh, you're on a web host. In that case, I misread your question. If you want to maintain an online highscore list, then the canonical implementation would be a MySQL database, on top of that a PHP web service (don't bother with SOAP, just send JSON data using GET and POST), and then on the client something around libcurl to access the web service.

A particularly nasty problem in this scenario is that you need to secure access to the web service, otherwise anyone can figure out how it works and send spoofed scores, pretty much voiding the entire purpose of the highscore list. You need to make sure that the request really comes from a legit client; unfortunately, there is no 100% secure solution, other than putting all the program logic on the server.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Jonatan Hedborg
Member #4,886
July 2004
avatar

A "good enough" way might be to send on a hash of the data with the request. Maybe a MD5 hash where every other character is xor'd with the other.

AMCerasoli
Member #11,955
May 2010
avatar

Well.... Following the Mark Oates suggestion... I'll use them all... Check this out.

Tools:

C++: We all know why I'm using this.
libCurl: To connect to the server.
PHP: To process the data sent by the program and receive information from the server and the database, which is in another server btw.
MySQL: To store info about High Scores and Question_Packages.
SQLite: People create their own question packages using SQLite, since the SQLite license is very nice I can ship my game along with SQLite without convert it to GPL (thing that I would have to do if I were using MySQL).

So the game should have:

- A SQLite database with the official Question_Packages.
- A panel which connects to the MySQL database and receive info of other "Question_Packages" created by the users. clicking in one of those package will start the downloading process, which will download a SQLite database, and since it's in just one file it's really easy.
- The same panel which shows the Question_Packages should tell you (someway) if you have already downloaded that package or if you don't.
- A panel which allows you to create a SQLite database and then upload it to the server, with fields like "Will your Question_Packages be editable" "username" "password", so if someone download that package and the creator want to make it editable other users are going to be able to add more questions.

I think the MySQL database should have the next fields:

ID, Ques_Pack_Name, URL_of_file, downloads, creation_data, update_data, amount_of_questions, it's_editable, password, Username, description ... and optionally in the same table the 15 best scores but I'm not sure.

The bad news is that there is a loooooooooooooooooooooooooooooot of work.

The good news is that I'm able to do it.

So what do you think? :o it's too elaborate and unnecessary complicated? or' it's fine.

bamccaig
Member #7,536
July 2006
avatar

It sounds reasonable, though I would probably eliminate the "editable" stuff and just make all user-submitted packages editable (i.e., open). It's not like you can really prevent it anyway.

Otherwise, your database schema sounds horribly de-normalized, but I'm too tired and/or lazy right now to actually think about it. :P You might as well just store a single username/password per user though rather than having them register a new one for each package. Similarly, you don't want to store the high scores in the same table as packages. Without thinking too hard, I can see at least 3 tables: user, package, and score.

Since you will be storing a password I feel obligated to point out that you should never store plain text passwords, especially of users. Salt and hash them (one-way cipher) and store that. Do the same to their inputs when you need to compare for authentication. You might also warn your users not to use "good" passwords so they don't use e.g., their online banking password with your game. :P

MiquelFire
Member #3,110
January 2003
avatar

For passwords, crypt with blowfish, 15 or 16 is good right now (later on, you'll need to update this value over time, but that would be in a few years)

The slowest time I have reported for 16 is around 7 seconds (and it's 3.5 seconds for 15)

---
Febreze (and other air fresheners actually) is just below perfumes/colognes, and that's just below dead skunks in terms of smells that offend my nose.
MiquelFire.red
If anyone is of the opinion that there is no systemic racism in America, they're either blind, stupid, or racist too. ~Edgar Reynaldo

Neil Walker
Member #210
April 2000
avatar

Have you looked into SQLite?

If the goal is to maintain a highscore list, then SQLite is overkill.

My high-score data stored in a csv, is read, split and sorted using standard STL containers/algorithms, no need for fancy database libraries. Only takes a few lines of code. I then upload scores and download high-score tables using libcurl and on uploading the server splits the csv and sticks it in a mysql table.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Go to: