2

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.

8
  • where is your database code? in general your code is correct, you must do INSERT into foreach loop. Commented Mar 22, 2016 at 11:06
  • Please have look at the code. I am using separate function for database in another file. Commented Mar 22, 2016 at 11:12
  • in general is OK, just do if (count($DecodingData) > 0) in your function as this is an array after explode(). one more suggestion - start using mysqli_* functions instead of mysql_* Commented Mar 22, 2016 at 11:15
  • 1
    one more thing - what is the purpose of $counter variable? every time when you call the function, the value will be 0, so you will try to insert every record with this 0. if this is a unique rowID in your database, make it autoincremental and remove it from SQL query OR make it global variable in your PHP script. most probably this is the problem Commented Mar 22, 2016 at 11:21
  • 1
    The list of things you should remove begins with your database code. mysql_* functions are not present in the current version of PHP and were deprecated more than 5 years ago. Just stop using them. Commented Mar 23, 2016 at 20:23

1 Answer 1

0

You will need to perform 2 separate e plosions to prepare the data.

Use a single, prepared statement to ensure that your query is stable and secure.

Here is a similar mysqli technique as a comparison.

I don't like the look of that $counter. Your database should have the row identifier as an autoincremented primary key and the date column should have a default value of CURRENT_TIMESTAMP so that that column doesn't need to be declared during insert queries.

I don't know what your column names are, so I cannot add them to my sql.

We shouldn't see mysql_ functions anymore; use mysqli_ or pdo functions.

Code:

$string = "Palace1,radio,location1,location2,location3,location4,GSMId:Palace2,radio,location1,location2,location3,location4,GSMId:Palace3,radio,location1,location2,location3,location4,GSMId";

$stmt = $mysqli->prepare("INSERT INTO gsmclient_test VALUES(?,?,?,?,?,?)";
$stmt->bind_param('ssssss', $pal, $rad, $loc1, $loc2, $loc3, $loc4);

foreach (preg_split('/,GSMId:?/', $string, -1, PREG_SPLIT_NO_EMPTY) as $rowString) {
    [$pal, $rad, $loc1, $loc2, $loc3, $loc4] = explode(',', $rowString, 6);
    $stmt->execute();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.