Call Data from MSQLi to gamesalad
Hi Good Day,
Here's my code of script that i also copied on the forum..I don't know what wrong with this:
<?php
$servername = "localhost";
$username = "root";
$password = "421gametest";
$dbName = "gsdata";
$port = "3306";
$tableName = "gsham";
$link = @mysqli_connect($servername, $username, $password, $dbName, $port);
// database connection strings. change these to your DB and Table names.
$db = new mysqli($servername, $username, $password, $dbName, $port);
if (mysqli_connect_errno()) {
printf("Connection failed in get.php %sn", mysqli_connect_error());
exit();
}
// connect to the table
//mysqli_select_db($link, $dbName)or die("cannot select DB");
// lets prepare some files to capture what is going on.
$incomingJson = 'json.txt';
//$fullArray = 'fullArray.txt'; // needed if you enable the debugging section below
$sqlErrorLog = "sqlErrors.txt";
// initialize the string with a blank value
$string = "";
// start GET data
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
// initialize the JSON body variable
$jsonBody="";
// get table contents
$query = sprintf("SELECT * FROM `%s`", $tableName);
$res = $db->query($query);
if ($res === false) {
$err = sprintf("Invalid query: %snWhole query: %sn", $db->error, $query);
throw new Exception($err);
}
// construct an array to hold the data we pull from mySQL
while($row = $res->fetch_array(MYSQLI_BOTH))
{
$rows[] = $row;
}
//$rows = $res->fetch_array(MYSQLI_BOTH);
// get the number of rows in the array. We need this in the JSON return
$arrlength = count($rows);
//loop through array node and get row values
for($i = 0; $i < $arrlength; $i++) {
$playerID = $rows[$i]["playerID"];
$playerName =$rows[$i]["playerName"];
$playerStats = $rows[$i]["playerStats"];
// table row numbers. our index starts at 0, so we want to increment it by 1 to get valid row numbers.
$tableRow = $i+1;
// construct the JSON return from our data
$jsonString = '{"Name":"'.$tableRow .'","Value":"|'.$playerID.'|'.$playerName.'|'.$playerStats.'|"},';
// append the JSON return with the new data
$jsonBody=$jsonBody.$jsonString;
//}
// increase index and loop again if not at end of array.
0
}
// construct the JSON response
// this is the header of the JSON return. It will have to be adjusted to match whatever your app is expecting. We have to define this here to get the row count above.
$jsonHeadher='{"Properties":[],"Name":"","Children":[{"Properties":[{"Name":"rowCount","Value":'.$arrlength.'},{"Name":"columnCount","Value":3},{"Name":"0-1-name","Value":"playerID"},{"Name":"0-1-type","Value":1},{"Name":"0-2-name","Value":"playerName"},{"Name":"0-2-type","Value":1},{"Name":"0-3-name","Value":"playerStats"},{"Name":"0-4-type","Value":2},{"Name":"0-4-name","Value":"auto_i"},{"Name":"0-3-type","Value":2}],"Name":"id826644_headers","Children":[]},{"Properties":[';
// this is the footer of the JSON return. Again it will have to be adjusted to match whatever your app is expecting.
$jsonFooter='],"Name":"id826644","Children":[]}]}';
// removes an extra comma that the loop above leaves behind
$jsonBody=rtrim($jsonBody, ",");
// constructing the full JSON return
$returnedJson=$jsonHeadher.$jsonBody.$jsonFooter;
// write the JSON data so the app can read it.
echo $returnedJson;
_______________________
here's the display on browser
dont know what the reason...
Please help
Cheers
Mongee
Comments
Here's my gs code
A big help for us to solve
Cheers
Mongee
FYI, github has a service called gist that will let you post code snippets with formatting and highlighting. For people who want code help in the future.
@adent42
Thanks so much for quick reply : ) would you mind to ask for some help on t ...what to do and whats wrong on it
Please i got stuck 4 days on it
Cheers
Mongee
First off, does the id match the table in the game?
@adent
Yes it is.....