how can i save score and name into free webhost...?

when i access into my webserver …

it is nesessary 

to have a username and pasword?

network.request( “http://mysite.com/api/login.php?playername=” … mime.b64(username) … “&password=” … mime.b64(password), “GET”, callback )

–then in the php file.

if(isset($_GET)) {
    $playername = base64_decode($_GET[“playername”]);
    $password = base64_decode($_GET[“password”]);
}

You have asked three questions here.  First, you could use the lua.socket library and open the MySQL database directly, but you would have to be very familiar with socket communications and the MySQL packet protocols, which even with my years of experience writing socket code, is not a project I would want to tackle, when a few lines of PHP will bridge the gap.  I would recommend using a PHP layer in between, even if you’re only going to have your app access it.

Do you need a username/password?  I suppose the technical answer is No, but do you really want anyone calling your script on your server and setting player scores?  You don’t need it to get a list of scores.  But for any script that writes you want to protect your database from being hacked.

Finally, the blog post was not a complete tutorial on saving scores.  It was to show you how you could get started with PHP, MySQL and Corona.  It’s up to you to figure out the rest.   You should spend time learning how HTTP GET requests work, how parameters are passed as part of the URL. 

Rob

sir ROB

i cant insert in my php,

what the problem of this…

–main.lua

local mime = require(“mime”)
local json = require(“json”)

local playername  = “dasiglangpls”
local playerscore = “200”

local URL = “http://1dermindstudios.comyr.com/submitscore.php?playername=”…mime.b64(playername)…"&playerscore="…mime.b64(playerscore)

local function loginCallback(event)
    if ( event.isError ) then
        print( “Network error!”)
    else

        print ( " success " )

    end

    return true
end
network.request( URL, “GET”, loginCallback )
 

–PHP

<?php
define(“DB_DSN”,‘a9679956_TR1’);
define(“DB_HOST”,‘mysql6.000webhost.com’);
define(“DB_USER”,‘a9679956_ian’);
define(“DB_PASS”,‘juario01’);
 
// Connecting, selecting database
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not connect: ’ . mysql_error());
mysql_select_db(DB_DSN) or die(‘Could not select database’);
 
if(isset($_GET)) {

    $playername  = base64_decode($_GET[“playername”]);
    $playerscore = base64_decode($_GET[“playerscore”]);

$query = ‘INSERT INTO player (playername,playerscore) VALUES ("’.mysql_real_escape_string($playername).’","’.mysql_real_escape_string($playerscore).’")’;
    

    $dbresult = mysql_query($query, $link);
    if (!$dbresult) {

        $result = array();
        $result[“result”] = 403;
        $result[“message”] = mysql_error();
 

        mysql_free_result($dbresult);
        exit;
    }

sir what is the problem of this?

attemt to index global ‘json’ (a nil value) ?

sir forget na previous question…
this is my last question…

when i used insert statement why null value to store…
this my main.lua

local myNewData
local json = require (“json”)
local decodedData

local playerName = “sos”
local playerscore = “200”

local function networkListener( event )
        if ( event.isError ) then
                print( “Network error!”)
        else
                myNewData = event.response
                print ("From server: "…myNewData)
                decodedData = (json.decode( myNewData))
   – SaveData()
        end
end
–end

local URL = “http://1dermindstudios.comyr.com/submitscore.php?playername=” …(playerName) … “&playerscore=” …(playerscore)
network.request( URL, “GET”, networkListener )

then my php file

<?php
$host=“mysql6.000webhost.com”;    
$username=“a9679956_caps2ne”;
$password=“blabla”;           
$db_name=“blabla”;           

$con=mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name") or die (“cannot select DB”);

mysql_select_db("$db_name") or die (“cannot select DB”);
$sql = “select * from player”;    //replace with your table name
$result = mysql_query($sql);
$json = array();
$count=0;
if(mysql_num_rows($result)){
while($row=mysql_fetch_row($result)) {
$count = $count+1;
$json[“movie”.$count]=$row;
}
}
mysql_close($db_name);
echo json_encode($json);
?>

You probably need to add some “echo” statements in your PHP to see what your values are.  echo is the same to php as print is to Lua.

Rob

sir where i can put echo?

$con=mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name") or die (“cannot select DB”);

if(isset($_GET)) {
    $playername =  ($_GET[“playername”]);
    $playerscore = ($_GET[“playerscore”]);

        
  
     $query = ‘INSERT INTO player (playername,playerscore) VALUES
("’.mysql_real_escape_string($playername).’","’.mysql_real_escape_string($playerscore).’")’;
        $result = mysql_query($query);
        echo json_encode($json);
        }
        exit

why i cant insert value with this statement?

i think theirs  a problem of this…

 

local playerName = “sos”
local playerscore = 200

local URL = “http://1dermindstudios.comyr.com/submitscore.php?playername=” …(playerName) … “&playerscore=” …(playerscore)
network.request( URL, “GET”, networkListener

I would add a:

print_r($_GET); 

just before the if(isset($_GET) line.  Then put

echo($playername);

echo($playerscore);

after you set those variables.  Finally do a:

echo($query);  

just before you call mysql_query($query).  You can run this in your browser to make sure its working.  In your Corona App, print your URL value just before you call network.request().  Copy that URL to a web browser’s location bar to run your script fro the browser to see  your output.

Rob

sir same like this?

 <?php

print_r($_GET);

if(isset($_GET)) {

echo($playername);
echo($playerscore);

    $playername =  ($_GET[“playername”]);
    $playerscore = ($_GET[“playerscore”]);

        
        $query = ‘INSERT INTO player (playername,playerscore) VALUES ("’.mysql_real_escape_string($playername).’","’.mysql_real_escape_string($playerscore).’")’;
        $result = mysql_query($query);
        echo json_encode($result);
        echo($query);
        }
        exit
?>
 

but why output same like this…?

 

Array() trueINSERT INTO player (playername,playerscore) VALUES ("","")

theirs no value inserted

this is my Main.lua

 
local myNewData
local json = require (“json”)
local decodedData

local playerName = “sos”
local playerscore = 200

local function networkListener( event )
        if ( event.isError ) then
                print( “Network error!”)
        else
                myNewData = event.response
                print ("From server: "…myNewData)
                decodedData = (json.decode( myNewData))

        end
end
local URL = “http://1dermindstudios.comyr.com/submitscore.php?playername=” …(playerName) … “&playerscore=” …(playerscore)
network.request( URL, “GET”, networkListener )

 

problem solve… yehey tnx sir rob :slight_smile:

sir ROB… why the out put of this… is delay… its normal?

local function networkListener( event )
        if ( event.isError ) then
                print( “Network error!”)
        else
                myNewData = event.response                           --------------------------------------------------------------------
                print ("From server: "…myNewData                     -----------IN THIS PART DELAY OUTPUT----------------
                decodedData = (json.decode( myNewData)) ---------------------------------------------------------------------

        end
end
local URL = “http://1dermindstudios.comyr.com/submitscore.php?playername=” …(playerName) … “&playerscore=” …(playerscore)
network.request( URL, “GET”, networkListener )

:slight_smile:

It has to get a response from the server first, so depending on the speed of the server and the amount of data being returned, the speed of the response will vary.

You should read up on networking before jumping in to the deep end :wink: