Any PHP gurus have a second to look at my code?

RabidParrotRabidParrot Formally RabidParrot.Member Posts: 956
edited November 2015 in Working with GS (Mac)

Hey guys, I have been having some trouble sending and receiving data from my AWS MYSQL.

I can get the www.example.com/get.php to show the json string but GameSalad won't get any data from the page

I can't send the data from GameSalad either. Each time I try to send or receive data the callback remains 0.

I know that the website url is correct because the callback stays at "0"

If I purposely put in the wrong url I get a "-1" which I would expect.

I have done testing to verify that I am connected to my RDS instance. The data transfer is the issue.

I also added the two files below because i know that below this the post will get pretty sloppy.

Thanks to anyone who can give me some feedback.

My Send.php


<?php $con = mysql_connect("•••••••", "•••••••", "•••••••"); if (!$con) { die('Could not connect: ' . mysql_error()); } echo "Connected...!"; $dbName = "carBinary"; $tableName = "carBinary"; error_reporting(E_ALL); ini_set('display_errors', '1'); echo "Hello world"; // connect to the table mysql_select_db($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 secton below $sqlErrorLog = "sqlErrors.txt"; // initialize the string with a blank value $string = "error"; // start SEND data if ($_SERVER['REQUEST_METHOD'] === 'POST') { //capture incoming data error_reporting(1); $sig = $_POST["sig"]; $jsondata = $_POST["params"]; // this line captures the sent data so you can figure out what you need to send back. file_put_contents($incomingJson,$jsondata); // this line tells the application that the data send was successful. echo '{"Status":"Success"}'; // convert JSON to an array $array = json_decode($jsondata, TRUE); /* // formats the array to view it easier $results = print_r($array,true); file_put_contents($fullArray,$results); */ //get the total number of objects in the array $arrlength = count($array['Children']['1']['Properties']); // set while loop index $i = 0; //loop through array node and get row values while ($i < $arrlength ) { // get row value $value = $array['Children']['1']['Properties'][$i]['Value']."n"; // convert delimited string to an array $arrayPieces = explode("|", $value); // get array values. This section would have to be modified to capture each value you are interested in.\ $rowName = $arrayPieces[0]; // this variable will be blank if you don't name your rows. \ $carBinary = $arrayPieces[1]; // construct SQL statement\ $sql="INSERT INTO ".$tableName."(carBinary)VALUES('$carBinary')"; // insert SQL statement\ $result=mysql_query($sql); // catch any errors\ if($result){ // if successful do nothing for now.\ } else { // if failure, write to custom log\ $sqlError = "Error writing to database\n"; file_put_contents($sqlErrorLog, $sqlError, FILE_APPEND); } $i++; } } // end of POST\ // close the SQL connection\ mysql_close($con); ?>






My Get.php

<?php $con = mysql_connect("•••••••", "•••••••", "•••••••"); /*if (!$con) { die('Could not connect: ' . mysql_error()); } echo "Connected...!";*/ $dbName = "carBinary"; $tableName = "carBinary"; // connect to the table\ mysql_select_db($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 secton below\ $sqlErrorLog = "sqlErrors.txt"; // initialize the string with a blank value\ $string = ""; error_reporting(E_ALL); ini_set('display_errors', '1'); echo "Hello world"; // start GET data\ if ($_SERVER['REQUEST_METHOD'] === 'GET') { // initialize the JSON body variable\ $jsonBody=""; // get table contents\ $query = mysql_query("SELECT * FROM ".$tableName); // construct an array to hold the data we pull from mySQL\ $rows = array(); // loop through the table and drop the data into the array\ while($row = mysql_fetch_assoc($query)) { $rows[] = $row; } // get the number of rows in the array. We need this in the JSON return\ $arrlength = count($rows); // set while loop index\ $i = 0; //loop through array node and get row values\ while ($i < $arrlength ) { // tables we are capturing\ $carBinary = $rows[$i]['carBinary']; // 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":"|'.$carBinary.'|"},'; // append the JSON return with the new data\ $jsonBody=$jsonBody.$jsonString; // increase index and loop again if not at end of array.\ $i++; } // 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":1}, {"Name":"0-1-name","Value":"carBinary"}, {"Name":"0-1-type","Value":2}, "Name":"id713709_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":"id713709","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; } // end of get\ // close the SQL connection\ mysql_close($con); ?>

Comments

  • RabidParrotRabidParrot Formally RabidParrot. Member Posts: 956
    edited November 2015

    No error files or text files are produced either. If that helps.

  • gurechangurechan Member, PRO Posts: 211

    Could be in your start data send section: if ($_SERVER['REQUEST_METHOD'] === 'POST')

    Try using: if (!empty($_POST))

    So rather than comparing the send method, you’re just checking to see if the post method is not empty. If it has data, it will run.

  • RabidParrotRabidParrot Formally RabidParrot. Member Posts: 956
    edited November 2015

    @gurechan said:
    Could be in your start data send section: if ($_SERVER['REQUEST_METHOD'] === 'POST')

    Try using: if (!empty($_POST))

    So rather than comparing the send method, you’re just checking to see if the post method is not empty. If it has data, it will run.

    Will do. Thanks!

    Edit: Unfortunately it didn't work.

  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408

    If you compare the json being returned against the json being sent, does it match except for the number of entries?

    I'm not sure how to do it on Aws, but t check out the Apache and MySQL error logs, they are serverside and not generated by the code, if an error is happening, it should have it logged

  • RabidParrotRabidParrot Formally RabidParrot. Member Posts: 956

    @jonmulcahy said:
    If you compare the json being returned against the json being sent, does it match except for the number of entries?

    I'm not sure how to do it on Aws, but t check out the Apache and MySQL error logs, they are serverside and not generated by the code, if an error is happening, it should have it logged

    Here is what I'm getting. It looks like it is connecting but not pulling the data.

  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408

    @RabidParrot said:

    @jonmulcahy said:
    If you compare the json being returned against the json being sent, does it match except for the number of entries?

    I'm not sure how to do it on Aws, but t check out the Apache and MySQL error logs, they are serverside and not generated by the code, if an error is happening, it should have it logged

    Here is what I'm getting. It looks like it is connecting but not pulling the data.

    Check the MySQL logs, make sure there are no errors there

Sign In or Register to comment.