Updating an array before inserting MySQL?
I am getting XML via php: // input and after using simpleXML it breaks the elements into variables and then what I want to do is add an array or create an array of variables every 30 seconds or so.
The reason is that this script will receive normal inputs, and instead of doing lots of mySQL updates or inserts, I guess this might be better for efficiency.
So, a couple of questions, if anyone has a moment.
1) there is a way to check for new input in php: // input. 2) is there a better way to do a re-check than a sleep function? 3) how to add / add these update variables to the array?
I haven't gone too far yet, so the code is not helpful, but if you can forgive me for the simplicity: -
function input() {
$xml = new SimpleXMLElement($input);
$session_id = $xml->session_id;
$ip = $xml->ip;
$browser = $xml->browser;
store($session_id, $ip, $browser);
}
function store() {
$session_id = array();
$ip = array();
$browser = array();
}
a source to share
If I understand you correctly, it seems you are trying to use PHP for a long running stateful program. Hope you know this: Typically PHP programs will not run for more than a few milliseconds, no more than a few seconds for a typical web application. Each time a resource is requested from a PHP handler, parsing starts over and there is no program state left over from previous execution. As a stateless environment, you must maintain the state. For this reason PHP is not designed to handle input that changes over time or to maintain state.
As said, the simplest way to add to an array is as follows:
$myarray[] = "newvalue";
or
$myarray['newkey'] = "newvalue";
To process a stream:
while (!feof($handle)){ $data = fgets($handle, 4096); }
a source to share