Request username from database

Get help using Construct 2

Post » Mon Jan 21, 2013 6:35 pm

Hi Guys,

Im working on a Highscore system.
But i want my website users to have their name and profile picture in my games, when they are logged in to my website.

How would i "get" username and/or image from a database?
Is it done through ajax or something completely else?
B
34
S
8
G
7
Posts: 498
Reputation: 7,753

Post » Tue Jan 22, 2013 8:21 am

Short answer: Yes.

Longer version:
This http://www.scirra.com/tutorials/346/online-high-score-table-ajax-php-mysql tutorial will give you basic information, you can modify script for your needs.

Example call:
[code]"http://website.com/get.php?name="&name[/code]
note: after name=" you should define where script looks for name, it can be global variable, instance variable or so. To get information out, I had following line in my PHP script
[code]$result = mysql_query("SELECT * FROM (table) WHERE (row) = '" . mysql_real_escape_string($_GET['name']) . "'");
$row = mysql_fetch_row($result);[/code]
(table) = name of database table.
(row) = row where to look for info.

Example
[code]$result = mysql_query("SELECT * FROM highscores WHERE name = '" . mysql_real_escape_string($_GET['name']) . "'");
$row = mysql_fetch_row($result);[/code]
In this script is looking from highscores table and checking "name" row to match player name and gets information from there. Then we want some data out from player mysql rows. We can do following

[code]$koe = stripslashes($row[3]);
echo $koe;[/code]
This would echo $koe value to browser which you can echo to game using "Ajax.LastData" method. So whats up with $koe? In "$koe = stripslashes($row[3])" line Im looking third row from database, here is

example setup:
highcores table: ID, name, score.
now when we look at this, "score" is third row which is why we echoed it since we wanted score.

At websites (where we we have this script) we echoed $koe Ajax.LastData got one value which was $koe (score from database). You can use this method to dig out information from database to your game, modify script for your needs. To save up some time, you can also do few if/elseif things in script to define what exactly script is looking.

Example
[code]"http://website.com/get.php?pid=score&name="&name[/code]
example code at PHP page.
[code]<?php if $_REQUEST["pid"] == "score") { mysql code for score search };
elseif ($_REQUEST["pid"] == "avatar") { mysql code for avatar search ; }?>[/code]

Now you have to only change "pid=score" line from ajax request to match score or avatar and your PHP script would echo information you need without need to setup more complex thing. To get image, I assume you could echo full URL, for example check this file: http://dl.dropbox.com/u/59009902/load_image_from_url.capx

Hopefully this helps abit, I assume you wanted more simple and short answer but I figured that small tutorial would help others who are looking for answer to same problem.

This is not perfect solution but worked for me, tho I was searching and taking larger amount of data but idea is same. Feel free to send me private message if you need more detailed help.CoffeeOD2013-01-22 08:29:02
B
7
S
1
Posts: 51
Reputation: 1,087

Post » Sat Dec 28, 2013 3:15 pm

A very helpful & simplified explanation @CoffeeOD ! Thank you!
B
15
S
6
G
3
Posts: 87
Reputation: 4,304


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 20 guests