Ask here your questions to get help using GDevelop.
By vjyvgns
#62551 Hello, I am trying to send a value from GDevelop game to mySQL DB.

I followed all the steps followed in the below tutorial but still unable to do so
http://wiki.compilgames.net/doku.php/gd ... 0c8447a92f

Steps on what i did:
1. Created a game project with one scene
2. Placed on Sprite in the scene
3. Created a Global Variable with a value
4. Wrote actions and events that on clicking the sprite send a request a webpage
5. Have specified all the localhost server details rightly and the Variable value in the Body content
6. Created the PHP files with the SQL queries to insert a value and select the value to display[/list]
Now, Facing error on the line where the variable is invoked to be defined to a variable

Code: Select all$score = $_POST['gscore'];
$sql="INSERT INTO user_highscores (User_Highscore) VALUES ($score)";


Note: gscore is the value i am trying to bring from GDevelop game. The error is that "Undefined index: gscore"
Only thing that i was unable to do from the tutorial is that setting up .htaccess file. I am not sure how to do. Is that the reason why this is not working?
Can someone please help me in bringing the value from the game to mySQL DB?
User avatar
By ddabrahim
#62558
"Undefined index: gscore"

I'm not an expert on PHP and MySQL but my guess would be that it simply does not receive the index 'gscore' from GDevelop.

The problem could be either the server doesn't allow the call made by GDevelop or you didn't setup the request in GDevelop properly or your firewall block the connection.

First make sure your firewall allow the game and GDevelop to connect.

Then make sure everything is set in GDevelop the way it supposed to be. You are expecting to receive 'gscore' so make sure you have entered in GDevelop "gscore="+value as request
The value can be anything really but make sure you convert it to string in case it an integer using the ToString() expression.
For example: "gscore="+ToString(Variable(score))

Next, make sure you have the "POST" method enabled

Finally, make sure you have the host and the page setup correctly.
The host field should NOT include any folders or pages. For example if you have a gdevelop folder in your web root directory and you have the php file inside the gdevelop folder, it should look like this to send a value from a variable to a php file:

Code: Select allHost:
"http://www.something.com" or "http://localhost"

Path to page:
"/gdevelop/insertScore.php"

Request body content:
"gscore="+ToString(Variable(score))

Method:
"POST"


Then, the php code should look something like this to receive the value and insert it in to the database:

Code: Select all<?php
    //connect to database
    $database = mysqli_connect('localhost', 'username' , 'password' , 'database_name');
    //if connection to database is successful, then get index 'gscore' sent from GDevelop and insert in to the database
    if($database){
       
        $score = $_POST['gscore'];
        if($score){
            $query="INSERT INTO table_name(column_name) VALUES ($score)";
            mysqli_query($database, $query);   
        }else{
            die(mysqli_error());
        }
       
    }else{
        die(mysqli_error());
    }

?>


If it still doesn't work, probably it is the server configuration, I'm not very experienced on this, so I can't offer help.
The only thing I can suggest to try is to add an .htaccess next to the PHP file which is supposed to receive the data 'gscore' and make the MySQL query and enter the following in to the .htaccess file:
Code: Select allHeader add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
Last edited by ddabrahim on Thu 13 Jul 2017, 18:57, edited 1 time in total.
User avatar
By Bouh
User avatar
By ddabrahim
#62567
still unable to bring the value to DB

Probably it doesn't help but just in case, I was forget to mention to use "http://" in the host name.
Instead of "www.something.com" it must be "http://www.something.com" otherwise it doesn't work...

GDevelop should be able to send the request and pass the value to php as I told you, and my php code should work too so if it doesn't work for you the only thing left that might be causing the problem is your firewall (in case it is an online hosting) or it is something on the server side but I can't really help you with that :(

Have you tried to write a fix value in to the database when GDevelop send the request to php?
If you haven't, I recommend to try.

For testing purposes only, Instead of trying to get and use a value passed from GDevelop try "VALUES(100)" in the query.
Code: Select all$query="INSERT INTO table_name(column_name) VALUES (100)";
mysqli_query($database, $query); 

If it works and it write 100 in to the database it would confirm that at least GDevelop is able to reach the website and send the request, and the PHP also works. So at least you know there is no problem with that and you can focusing on why GDevelop can not pass the value to php.

In case it doesn't work, you can't even write a fix value in to the database by sending a request from GDevelop, that means GDevelop can't even reach the website or send the request for some reason or php unable to connect to the database. So you have much bigger problems and you need to start from scratch...
By vjyvgns
#62640
ddabrahim wrote:still unable to bring the value to DB
Probably it doesn't help but just in case, I was forget to mention to use "http://" in the host name.
Instead of "www.something.com" it must be "http://www.something.com" otherwise it doesn't work...

Yup. I already made sure to test with and without 'http://'. Still it doesn't work.

ddabrahim wrote:might be causing the problem is your firewall (in case it is an online hosting) or it is something on the server side but I can't really help you with that

I am running my PHP in localhost and I am currently checking the firewall settings and inspecting from that angle.

For testing purposes only, Instead of trying to get and use a value passed from GDevelop try "VALUES(100)" in the query.
Yup. Already tested this and it works. The problem is only with the value not passing from GDev..

Thanks for your input! Really appreciate it..
By vjyvgns
#62641
Bouh wrote:If you want i created a tutorial for this, the example can be downloadable at the bottom
http://www.wiki.compilgames.net/doku.ph ... sededonnee


Thanks for this tutorial. I have downloaded and tested it. I still dont see the values passing from GDev. I tried to pass a value directly without calling from the value entered in Scene (textChamp) still no luck. As ddabrahim already pointed out to check if any setting with firewall needs to be updated. i am currently checking from that angle.

Thanks for your tutorial again and appreciate your help!
User avatar
By Bouh
#62657 Very strange, if you want check my prototype about nodeJS and GDevelop :
https://github.com/Bouh/Prototypes

All my variable in GD are push to server and the server broadcast the variable to all players.
It's not mySQL but you can display/show the variables in the scenes