How to GET data from MySql server?

I am working on a very important project using Arduino Uno and SIM900 GSM Module. Basically I correctly read the ID from an RFID Card(RFID-RC522) and I need to send the ID using an URL to the Host Provider’s Database(000webhost) and I need to get the OUTPUT of my query but as you will see a simple change in the code and the output is completely changed.

Here is the code for the PHP file that needs to send the OUTPUT:

<?php
$dbhost = "localhost";
 $dbuser = "*******";
 $dbpass = "*******";
 $db = "********";
 $conn = new mysqli($dbhost, $dbuser, $dbpass,$db);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
mysqli_select_db($conn,$db);
date_default_timezone_set('Europe/Rome');
$date = date("Y-m-d G:i:s");
$code=$_REQUEST['code'];

$sql_u = "SELECT * FROM users WHERE code='$code'";
$res_u = mysqli_query($conn, $sql_u);
if (mysqli_num_rows($res_u) > 0) 
{
    echo "ALREADY EXISTENT $code";

}
else
{
    $sql = "INSERT INTO users(code,used) VALUES ('$code','$date')";
    if (mysqli_query($conn, $sql)) {
        echo "SUCCESS $code";

    } else {
        echo "ERROR INSERTING $code";

    }
}


mysqli_close($conn);
?>

Anyways, here is the Arduino Code:

#include <SoftwareSerial.h>
#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN 9
#define SS_PIN 10

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance

SoftwareSerial myGsm(7, 8);

String UID = "";
String pUID;

void setup() {
  Serial.setTimeout(100);
  SPI.begin(); // Init SPI bus
  mfrc522.PCD_Init(); // Init MFRC522

  myGsm.begin(19200);
  Serial.begin(9600);

  myGsm.println("AT+CGATT=1");
  printSerialData();

  myGsm.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
  printSerialData();

  myGsm.println("AT+SAPBR=3,1,\"APN\",\"internet.wind\"");
  printSerialData();

  myGsm.println("AT+SAPBR=2,1");
  printSerialData();

  myGsm.println("AT+SAPBR=1,1");
  printSerialData();

  myGsm.println("AT+HTTPINIT");
  printSerialData();
}

void loop() {
  if (!mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if (!mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  pUID = UID;
  UID = mfrc522.PICC_DumpDetailsToSerialUid(&(mfrc522.uid));
  if (UID != pUID) {
    Serial.print("RFID Code: ");
    Serial.print(UID);
    Serial.print("\n");
    String url = "http://mywebsite.000webhostapp.com/test.php?code=", code = "";

    url += UID;
    code = "";
    String httpara = "";
    httpara += "AT+HTTPPARA=\"URL\",";
    httpara += url;
    url = "http://mywebsite.000webhostapp.com/test.php?code=";
    myGsm.println(httpara); // setting the httppara, 
    printSerialData();

    myGsm.println("AT+HTTPPARA=\"CID\",1");
    printSerialData();

    myGsm.println("AT+HTTPACTION=0"); //submit the GET request 
    //delay(8000);//the delay is important if the return datas are very large, the time required longer.
    printSerialData();

    myGsm.println("AT+HTTPREAD=0,20"); // read the data from the website you access
    //delay(3000);
    printSerialData();

  }

}

void printSerialData() {

  String readI = myGsm.readStringUntil("\r\n");
  Serial.println(readI);
}

The OUTPUT for this code is:

AT+CGATT=1

OK

AT+SAPBR=3,1,"CONTYPE","GPRS"

OK

AT+SAPBR=3,1,"APN","internet.wind"

OK

AT+SAPBR=2,1

+SAPBR: 1,3,"X.X.X.X"

OK

AT+SAPBR=1,1


OK

RFID Code: 172D9B32
AT+HTTPPARA="URL",http://mywebsite.000webhostapp.com/test.php?code=172D9B32

OK

AT+HTTPPARA="CID",1

OK

AT+HTTPACTION=0

OK

AT+HTTPREAD=0,20

OK

If you take a closer look at the last part of the Arduino code, under the line myGsm.println("AT+HTTPACTION=0"); and under the line myGsm.println("AT+HTTPREAD=0,20"); there are some delays that almost all the other SIM900 examples use… I didn’t use the delays but with the help from other people, I made it in a way that as soon as the SIM900 has an OUTPUT it just prints it out and eliminating the delay this way. It works fine for all the other commands, but for the last 2 commands it just does something random I think because it should give me the result from the echo of the php file…

NOW look if I enable the two delays:

myGsm.println("AT+HTTPACTION=0"); //submit the GET request 
delay(8000);//the delay is important if the return data are very large, the time required longer.
printSerialData();

myGsm.println("AT+HTTPREAD=0,20"); // read the data from the website you access
delay(3000);
printSerialData();

By enabling these 2 line now the OUTPUT is this(correct):

AT+CGATT=1

OK

AT+SAPBR=3,1,"CONTYPE","GPRS"

OK

AT+SAPBR=3,1,"APN","internet.wind"

OK

AT+SAPBR=2,1

+SAPBR: 1,1,"X.X.X.X"

OK

AT+SAPBR=1,1

OK

AT+HTTPINIT

OK

RFID Code: 172D9B32
AT+HTTPPARA="URL",http://mywebsite.000webhostapp.com/test.php?code=172D9B32

OK

AT+HTTPPARA="CID",1

OK

AT+HTTPACTION=0

OK

+HTTPACTION:0,200,16

AT+HTTPREAD=0,20

+HTTPREAD:16
SUCCESS 172D9B32
OK