title: Sending result of mysql select query from php to python through a tcp socket
body: <p>I'm trying to send the result of my MYSQL select query to my Rpi server using PHP through the TCP socket.</p>
  
  <p><strong>Here is the PHP code:</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body&gt;
  
  &lt;?php
  $servername = "localhost";
  $username = "";
  $password = "";
  $dbname = "fyp_lora";
  
  $host = "localhost";
  $port = 12345;
  
  $f = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  socket_set_option($f, SOL_SOCKET, SO_SNDTIMEO, array('sec' =&gt; 1, 'usec' 
  =&gt; 500000));
  $s = socket_connect($f, $host, $port);
  
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
      die("Connection failed: " . $conn-&gt;connect_error);
  } 
  
  $sql = "SELECT * FROM led_control";
  $result = $conn-&gt;query($sql);
  $data = array();
  
  
  if ($result-&gt;num_rows &gt; 0) {
      // output data of each row
      while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
          $data[] = $row;
          echo $row["ID"]. ",". $row["LED"]. "," . $row["Status"] . "&lt;br&gt;";
  
      }
  } else {
      echo "0 results";
  }
  
  $conn-&gt;close();
  foreach($data as $value)
  {
  $msg= $value["ID"]. ",". $value["LED"]. "," . $value["Status"] . "&lt;br&gt;";
  $len = strlen($msg);
  while (true) {
          print($msg);
          $sent = socket_write($f, $msg, $len);
  
          if ($sent === false) {
  
              break;
          }
  
          // Check if the entire message has been sent
          if ($sent &lt; $len) {
  
              // If not send the entire message.
              // Get the part of the message that has not yet been sent as message
          $msg = substr($msg, $sent);
  
          // Get the length of the not sent part
          $len -= $sent;
  
      } else {
          socket_close($f);
          break;
      }
  
  }
  }
  ?&gt; 
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Here is the python code as referenced by:</strong>
  <a href="https://stackoverflow.com/questions/24005774/sending-a-message-from-php-to-python-through-a-socket">Sending a message from PHP to Python through a socket</a></p>
  
  <pre><code>import socket
  
  s = socket.socket()
  host = "localhost"
  port = 12345
  s.bind((host, port))
  
  s.listen(5) #allows for 5 connections to be established
  while True:
      c, addr = s.accept()
      data = c.recv(1024)
      if data: print (str(data.decode().split(",")))
      c.close() #closes the socket
  </code></pre>
  
  <p>On the Rpi server, it will only receive the 1st row of the message for example: <strong>['1', 'LED 1', 'OFF']</strong> but it does not receive the rest of the message.</p>
  
  <p>The full message would be as follows:
  <br>1,LED 1,OFF
  <br>2,LED 2,OFF
  <br>3,LED 3,OFF
  <br>4,LED 4,ON</p>
  
  <p>I would greatly appreciate any help given :)</p>