I am new to PHP apologize if this is an unworldly question. I am receiving a packet of data with dynamic length on the tcp/ip socket. The packet looks like this:
Palace1,radio,location1,location2,location3,location4,GSMId:Palace2,radio,location1,location2,location3,location4,GSMId:Palace3,radio,location1,location2,location3,location4,GSMId
You can see after the GSMId
I have a colon to separate one reports. The length of the packet could be anything.
My task is that I want to chop this packet after every colon (:) and want to save each report in Database.
Right now what I am doing to chop each packet is:
$string = "Palace1,radio,location1,location2,location3,location4,GSMId:Palace2,radio,location1,location2,location3,location4,GSMId:Palace3,radio,location1,location2,location3,location4,GSMId";
$countString = substr_count($string, ":");
$NumberOfReports = $countString + 1;
echo $NumberOfReports."\n";
echo $countString."\n";
$chopPacket = explode(':' , $string);
foreach($chopPacket as $value)
{
$Report = $value;
echo $Report."\n";
writeToDataBase($Report);
}
DataBAse Code :
function writeToDataBase($Report)
{
date_default_timezone_set("Europe/London");
$date = date('Y-m-d H:i:s');
$counter = 0;
$DecodingData = explode("," , $Report);
if ($DecodingData > 0) {
$username = "user";
$password = "password";
$host = "localhost";
$connector = @mysql_connect($host, $username, $password) or die("Unable to connect");
$selected = @mysql_select_db("gsmdb", $connector) or die("Unable to connect");
$importSQL = "INSERT INTO gsmclient_test VALUES('".$counter."','".$DecodingData[0]."','".$DecodingData[1]."','".$DecodingData[2]."','".$DecodingData[3]."','".$DecodingData[4]."', '".$DecodingData[5]."','".$DecodingData[6]."','".$date."')";
mysql_query($importSQL) or die(mysql_error());
mysql_close($connector);
}
}
The code above is only saving the first report in database.