Sending a value from GDevelop to mySQL DB

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
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
$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?

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]Host:
http://www.something.com” or “http://localhost

Path to page:
“/gdevelop/insertScore.php”

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

Method:
“POST”
[/code]

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

[code]<?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());
}

?>[/code]

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:

Header add Access-Control-Allow-Origin "*" Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

Hi ddabrahim, Thanks for your input. I tried everything as you suggested and still unable to bring the value to DB.
Facing the same issue as score is not being passed from GDevelop. :frowning:

Thanks,

If you want i created a tutorial for this, the example can be downloadable at the bottom
wiki.compilgames.net/doku.ph … sededonnee

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 :frowning:

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.

$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…

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

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

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…

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!

Very strange, if you want check my prototype about nodeJS and GDevelop :
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

Thanks to everyone who contributed to this post. I have managed to find a solution.
I hope this post is useful for anyone who faces a similar issue like i did…

Thanks,
Vijay Vignesh

Thanks for sharing prototype. It was helpful…
I have managed to fix the issue and values are now being passed from GDev to our MySQL Db now

Thanks again!

I’m happy for you.
You’re welcome.

Hello, could you share your solution with me? I am having the same problem.

Regards :smiley:

Hi!

I also have the same problem. Could you help me sort this out please?

Regards,

Lore.

congrats for solving your problem.

If you don´t mind, would you share your solution?