?
Solved

Double data inserts

Posted on 2009-04-22
14
Medium Priority
?
259 Views
Last Modified: 2013-12-12
I have the following PHP code inserting the same data twice into a MySQL table with the folowing format:

CREATE TABLE IF NOT EXISTS `node_a_data` (
  `node_a_data_id` int(5) NOT NULL auto_increment,
  `timestamp` varchar(15) NOT NULL default '',
  `user_count` int(5) NOT NULL default '0',
  PRIMARY KEY  (`node_a_data_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=971 ;

Data get inserted twice, with the first insert with user_count as '0' and then mismatching the data (as well as doing double inserts:

(1, '1240230900533', 0),
(2, '1240230900533', 255),
(3, '1240231200608', 255),
(4, '1240231200608', 285),
(5, '1240231500534', 285),
(6, '1240231500534', 319),
(7, '1240231800550', 319),
(8, '1240231800550', 343),
(9, '1240232100544', 343),
(10, '1240232100544', 370),
(11, '1240232400537', 370),
(12, '1240232400537', 390),
(13, '1240232700545', 390),
(14, '1240232700545', 393),
(15, '1240233000535', 393),
(16, '1240233000535', 379),
(17, '1240233300551', 379),
(18, '1240233300551', 359),
(19, '1240233600558', 359),
(20, '1240233600558', 355),
(21, '1240233900539', 355),

<?php
require_once('connection/connect.php');
mysql_select_db($database, $connection);
 
$matches=array('timestamp=', 'Current user count:'); 
 
$log = "webct_monitor.log";
 
$openLog = fopen($log, 'r') or exit('Unable to open file!');  
while(! feof($openLog)) {  
$newline=fgets($openLog);
	foreach($matches as $match){  
		if(strstr($newline, $match)){   
			switch ($match){  
			case 'timestamp=':  
			$newline1 = explode($match, $newline);  
			$substring = explode('time=', $newline1[1]); 
			$timeStamp = str_replace('"','',$substring[0]);
			break;  
			case 'Current user count:':  
			$substring = explode($match, $newline);  
			$userCount = $substring[1];  
			break;  
			} 
			$query = "INSERT INTO node_a_data (node_a_data_id,timestamp,user_count) VALUES ('','$timeStamp','$userCount')";
$result = mysql_query($query, $connection) or die(mysql_error());
		}  
	}  
}  
fclose($openLog);  
?>

Open in new window

0
Comment
Question by:peterbrowne
  • 7
  • 7
14 Comments
 
LVL 9

Expert Comment

by:richdiesal
ID: 24212240
It is double inserting because of your code structure.  This line:

foreach($matches as $match){  

...tells PHP to process everything in the next set of brackets for each element in your array.  Your array has two elements ('timestamp=', 'Current user count:'), so it processes everything twice.  You should just get rid of that loop.  It doesn't look like you need it.  See snippet.
<?php
require_once('connection/connect.php');
mysql_select_db($database, $connection);
 
$matches=array('timestamp=', 'Current user count:'); 
 
$log = "webct_monitor.log";
 
$openLog = fopen($log, 'r') or exit('Unable to open file!');  
while(! feof($openLog)) {  
$newline=fgets($openLog);
                if(strstr($newline, $match)){   
                        switch ($match){  
                        case 'timestamp=':  
                        $newline1 = explode($match, $newline);  
                        $substring = explode('time=', $newline1[1]); 
                        $timeStamp = str_replace('"','',$substring[0]);
                        break;  
                        case 'Current user count:':  
                        $substring = explode($match, $newline);  
                        $userCount = $substring[1];  
                        break;  
                        } 
                        $query = "INSERT INTO node_a_data (node_a_data_id,timestamp,user_count) VALUES ('','$timeStamp','$userCount')";
$result = mysql_query($query, $connection) or die(mysql_error());
                }  
}  
fclose($openLog);  
?>

Open in new window

0
 
LVL 9

Expert Comment

by:richdiesal
ID: 24212243
I am assuming, by the way, that each line of that WebCT log has both "timestamp=" and a "Current user count:" component.  We could verify that if you post a snippet of the WebCT log though.
0
 

Author Comment

by:peterbrowne
ID: 24212281
Yes, the log repeats, see snippet.  I have tested your code and nothing is inserted...

BTW, this similar code repeats table rows with the same data with duplicating the data.

$matches=array('timestamp=', 'Current user count:');

$log = "webct_monitor.log";

echo '<table><tr><th>Time</th><th>User Count</th></tr>';
$openLog = fopen($log, 'r') or exit('Unable to open file!');  
while(! feof($openLog)) {  
$newline=fgets($openLog);
      foreach($matches as $match){  
            if(strstr($newline, $match)){  
                  switch ($match){  
                  case 'timestamp=':  
                  $newline1 = explode($match, $newline);  
                  $substring = explode('time=', $newline1[1]);
                  echo '<tr><td>' . date('m-d-y H:i:s', substr($substring[0],1,-5)) . '</td>';  
                  break;  
                  case 'Current user count:':  
                  $substring = explode($match, $newline);  
                  echo '<td>' . ereg_replace('[^0-9]','',$substring[1]) . '</td></tr>';  
                  break;  
                  }  
            }  
      }  
}  
echo '</table>';  




<log4j:event logger="VistaMonitor" timestamp="1240230900533" time="Mon Apr 20 22:35:00 EST 2009" level="INFO" thread="VistaPooledThread-16:2431" schema="LMS">
<log4j:message><![CDATA[
 
Background Job Service:  Available number of worker threads: 5
Background Job Service:  Number of started jobs per thread: 2911,2065,1394,1543,2279
Background Job Service:  Number of completed jobs per thread: 2911,2065,1394,1543,2279
Background Job Service:  Number of failed jobs per thread: 0,0,0,0,0
Background Job Service:  Total number of pending jobs: 0
Background Job Service:  Total number of started jobs: 10192
Background Job Service:  Total number of completed jobs: 10192
Background Job Service:  Total number of failed jobs: 0
Background Job Service:  Completed jobs: 100%
 
WebCT Session:  Current user count: 255
WebCT Session:  Active user sessions in last 5/10/15 minutes: 89/144/189
WebCT Session:  Average requests/minute for the last 5 minutes: 1288
Http Session:  Current number of open sessions: 3191
Http Session:  High water mark of the total number of open sessions: 2180
Http Session:  Total number of sessions opened: 979492
 
JobScheduler:  Number of jobs currently scheduled (Max) = 122(122)
 
Cache Service:   Number of cache currently used: 39
   FMSDK.GET_LC_BY_PATH: size: 0; requests: 632346; hits: 49.88%; misses: 50.12%
   INVENTORY_ORDER_MAP_CACHE: size: 5131; requests: 165420; hits: 85.64%; misses: 14.36%
   LCTERM.SOURCEDID: size: 6; requests: 382; hits: 64.92%; misses: 35.08%
   LEARNING_CONTEXT.LCTYPE: size: 6; requests: 6738; hits: 46.35%; misses: 53.65%
   lc.smsGroupId.AuthenticationSource: size: 2; requests: 128399; hits: 100.0%; misses: 0.0%
   cms.ContentEntry.rightwardLinks: size: 92878; requests: 2756911; hits: 96.38%; misses: 3.62%
   LEARNING_CONTEXT.XLIST_CHILD_ID: size: 0; requests: 0; hits: --; misses: --
   EDUCATOR_TO_PERSON: size: 429; requests: 1797377; hits: 99.72%; misses: 0.28%
   test: size: 0; requests: 0; hits: --; misses: --
   webct.mail.selectTarget.RoleItemList: size: 1; requests: 592; hits: 99.83%; misses: 0.17%
   SR.SR_LIGHT_VO: size: 4999; requests: 55249864; hits: 83.99%; misses: 16.01%
   LEARNING_CONTEXT.SOURCEDID: size: 999; requests: 32838663; hits: 99.92%; misses: 0.08%
   cms.ContentEntry.leftwardLinks: size: 63697; requests: 1573625; hits: 95.78%; misses: 4.22%
   LEARNING_CONTEXT.LC_SUBORDINATES: size: 1710; requests: 3771864; hits: 99.95%; misses: 0.05%
   PF.sms.groupId.groupName: size: 0; requests: 0; hits: --; misses: --
   permission.lightweightAclCache: size: 1621; requests: 159671; hits: 99.6%; misses: 0.4%
   ANNOUNCEMENT.ROLES.EACH.LCTYPE: size: 1; requests: 227; hits: 99.56%; misses: 0.44%
   ORGANIZER_PAGE_REGION: size: 4999; requests: 1751472; hits: 89.63%; misses: 10.37%
   LEARNING_CONTEXT.XLIST_MASTER_ID: size: 0; requests: 0; hits: --; misses: --
   LEARNING_CONTEXT.ID: size: 9999; requests: 201646272; hits: 99.55%; misses: 0.45%
   SR.CRITERIA_CLASS: size: 999; requests: 5577764; hits: 95.19%; misses: 4.81%
   lc.sourceName.smsGroupId: size: 1; requests: 624; hits: 54.97%; misses: 45.03%
   DISCUSSION_MESSAGE: size: 999; requests: 841516; hits: 69.12%; misses: 30.88%
   sms.groupId.groupName: size: 1; requests: 245; hits: 99.59%; misses: 0.41%
   cms.TypeRegistry: size: 1; requests: 251168787; hits: 100.0%; misses: 0.0%
   TOOL_USED_STATES: size: 999; requests: 10791751; hits: 99.84%; misses: 0.16%
   TOOL_VISIBLE_STATES: size: 2; requests: 10; hits: 0.0%; misses: 100.0%
   webct.mail.selectTarget.RoleRoleResStrMap: size: 1; requests: 1; hits: 0.0%; misses: 100.0%
   course.menu.palette: size: 999; requests: 386385; hits: 82.29%; misses: 17.71%
   LCTERMS_CACHE_AREA: size: 1; requests: 39; hits: 97.44%; misses: 2.56%
   cms.ContentEntry.childIds: size: 99999; requests: 47424889; hits: 96.82%; misses: 3.18%
   cms.ContentEntry: size: 99999; requests: 1247440708; hits: 95.7%; misses: 4.3%
   webct.mail.lc.role.userList: size: 99; requests: 0; hits: --; misses: --
   used.tool.ids: size: 999; requests: 386386; hits: 82.27%; misses: 17.73%
   cms.ContentEntry.localEntries: size: 20640; requests: 38636; hits: 44.64%; misses: 55.36%
   PORTFOLIO_ORG_MAP_CACHE_REGION: size: 0; requests: 0; hits: --; misses: --
   ANNOUNCEMENT.MYWEBCT.CHANNEL.LCTYPES: size: 1; requests: 202632; hits: 100.0%; misses: 0.0%
   ORG_MAP_CACHE_REGION: size: 999; requests: 1218307; hits: 97.1%; misses: 2.9%
   lc.AuthenticationSources: size: 1; requests: 282; hits: 99.65%; misses: 0.35%
 
Database:  Current number of active connections: 3
Database:  High water mark of active connections: 39
Database:  Total number of JDBC connections: 50
Database:  Number of leaked connections: 0
Database:  Averaged time necessary to get a connection (milliseconds): 87
Database:  Current total number waiting for a connection: 0
Database:  High water mark of waiters for a connection: 0
Database:  Number of seconds the longest waiter for a connection waited: 0
 
JMS:  Current connection count to this Server (WebCTManagedNodeJ): 14
JMS:  Peak connection count to this Server (WebCTManagedNodeJ): 15
JMS:  Total connection count to this Server (WebCTManagedNodeJ): 5086
JMS Destination Counts:  current consumers, high consumers, current messages, high messages, pending messages, received messages
 JMS: IntegrationFrameworkJMSServer.InboundQueue:  -1,-1,-1,-1,-1,-1
 JMS: IntegrationFrameworkJMSServer.OutboundQueue:  -1,-1,-1,-1,-1,-1
 JMS: IntegrationFrameworkJMSServer.ErrorQueue:  -1,-1,-1,-1,-1,-1
 JMS: ChatWBJMSServer.ChatWB.RoomTopic:  -1,-1,-1,-1,-1,-1
 JMS: ChatWBJMSServer.ChatWB.RoomTopicLogger:  -1,-1,-1,-1,-1,-1
 JMS: ChatWBJMSServer.WebctEventTopic:  -1,-1,-1,-1,-1,-1
 JMS: ChatWBJMSServer.LicensingIssueQueue:  -1,-1,-1,-1,-1,-1
 JMS: ChatWBJMSServer.MediaLibraryChange:  -1,-1,-1,-1,-1,-1
 
XSLT Pool:  Allocated number of objects: 0,0,0,0,0,0,0,0,0,0
XSLT Pool:  Available (free) number of objects: 10,10,10,10,10,10,10,10,10,10
XSLT Pool:  High water mark of the total number of allocated objects: 2,2,2,2,2,2,2,2,3,2
XSLT Pool:  Low water mark of the total number of available (free) objects: 8,8,8,8,7,8,7,7,7,8
XSLT Pool:  Average allocated objects, per hour: 0,0,555,556,555,0,593,593,594,593
XSLT Pool:  Average available (free) objects, per hour: 0,0,555,556,555,0,593,593,593,593
Tracking Queue: Current/Maximum/Total in last window: 0/1/916
 
 
JVM Monitor:  Heap size (bytes): 1610547200
JVM Monitor:  Available memory in heap (bytes): 565698088
 
ChatWB:  No data detected, chat server may not running.
 
Guest Session Pool:  Allocated number of guest sessions: 0
Guest Session Pool:  Available (free) number of guest sessions: 25
Guest Session Pool:  High water mark of the total number of allocated guest sessions: 6
 
Vista Application:  Current number of sockets registered for socket muxing: 1546
Vista Application:  Current number of waiting requests in the self-tuning thread pool: 0
Vista Application:  Total number of threads in the self-tuning thread pool: 28
Vista Application:  Current total number of alive servers in this cluster: 8
]]></log4j:message>
</log4j:event>
 
<log4j:event logger="VistaMonitor" timestamp="1240231200608" time="Mon Apr 20 22:40:00 EST 2009" level="INFO" thread="VistaPooledThread-0:0" schema="LMS">
<log4j:message><![CDATA[
 
Background Job Service:  Available number of worker threads: 5
Background Job Service:  Number of started jobs per thread: 2912,2065,1394,1543,2279
Background Job Service:  Number of completed jobs per thread: 2912,2065,1394,1543,2279
Background Job Service:  Number of failed jobs per thread: 0,0,0,0,0
Background Job Service:  Total number of pending jobs: 0
Background Job Service:  Total number of started jobs: 10193
Background Job Service:  Total number of completed jobs: 10193
Background Job Service:  Total number of failed jobs: 0
Background Job Service:  Completed jobs: 100%
 
WebCT Session:  Current user count: 285
WebCT Session:  Active user sessions in last 5/10/15 minutes: 97/140/190
WebCT Session:  Average requests/minute for the last 5 minutes: 1424
Http Session:  Current number of open sessions: 3212
Http Session:  High water mark of the total number of open sessions: 2180
Http Session:  Total number of sessions opened: 979588
 
JobScheduler:  Number of jobs currently scheduled (Max) = 122(122)
 
Cache Service:   Number of cache currently used: 39
   FMSDK.GET_LC_BY_PATH: size: 0; requests: 632346; hits: 49.88%; misses: 50.12%
   INVENTORY_ORDER_MAP_CACHE: size: 5131; requests: 165436; hits: 85.64%; misses: 14.36%
   LCTERM.SOURCEDID: size: 6; requests: 382; hits: 64.92%; misses: 35.08%
   LEARNING_CONTEXT.LCTYPE: size: 6; requests: 6738; hits: 46.35%; misses: 53.65%
   lc.smsGroupId.AuthenticationSource: size: 2; requests: 128399; hits: 100.0%; misses: 0.0%
   cms.ContentEntry.rightwardLinks: size: 92878; requests: 2757712; hits: 96.38%; misses: 3.62%
   LEARNING_CONTEXT.XLIST_CHILD_ID: size: 0; requests: 0; hits: --; misses: --
   EDUCATOR_TO_PERSON: size: 429; requests: 1797530; hits: 99.72%; misses: 0.28%
   test: size: 0; requests: 0; hits: --; misses: --
   webct.mail.selectTarget.RoleItemList: size: 1; requests: 592; hits: 99.83%; misses: 0.17%
   SR.SR_LIGHT_VO: size: 4999; requests: 55269411; hits: 83.99%; misses: 16.01%
   LEARNING_CONTEXT.SOURCEDID: size: 999; requests: 32838720; hits: 99.92%; misses: 0.08%
   cms.ContentEntry.leftwardLinks: size: 63698; requests: 1574085; hits: 95.78%; misses: 4.22%
   LEARNING_CONTEXT.LC_SUBORDINATES: size: 1710; requests: 3772537; hits: 99.95%; misses: 0.05%
   PF.sms.groupId.groupName: size: 0; requests: 0; hits: --; misses: --
   permission.lightweightAclCache: size: 1621; requests: 159671; hits: 99.6%; misses: 0.4%
   ANNOUNCEMENT.ROLES.EACH.LCTYPE: size: 1; requests: 227; hits: 99.56%; misses: 0.44%
   ORGANIZER_PAGE_REGION: size: 4999; requests: 1751761; hits: 89.63%; misses: 10.37%
   LEARNING_CONTEXT.XLIST_MASTER_ID: size: 0; requests: 0; hits: --; misses: --
   LEARNING_CONTEXT.ID: size: 9999; requests: 201679137; hits: 99.55%; misses: 0.45%
   SR.CRITERIA_CLASS: size: 999; requests: 5579288; hits: 95.19%; misses: 4.81%
   lc.sourceName.smsGroupId: size: 1; requests: 624; hits: 54.97%; misses: 45.03%
   DISCUSSION_MESSAGE: size: 999; requests: 841577; hits: 69.12%; misses: 30.88%
   sms.groupId.groupName: size: 1; requests: 245; hits: 99.59%; misses: 0.41%
   cms.TypeRegistry: size: 1; requests: 251230816; hits: 100.0%; misses: 0.0%
   TOOL_USED_STATES: size: 999; requests: 10794563; hits: 99.84%; misses: 0.16%
   TOOL_VISIBLE_STATES: size: 2; requests: 10; hits: 0.0%; misses: 100.0%
   webct.mail.selectTarget.RoleRoleResStrMap: size: 1; requests: 1; hits: 0.0%; misses: 100.0%
   course.menu.palette: size: 999; requests: 386491; hits: 82.29%; misses: 17.71%
   LCTERMS_CACHE_AREA: size: 1; requests: 39; hits: 97.44%; misses: 2.56%
   cms.ContentEntry.childIds: size: 99999; requests: 47431379; hits: 96.82%; misses: 3.18%
   cms.ContentEntry: size: 99999; requests: 1247725188; hits: 95.7%; misses: 4.3%
   webct.mail.lc.role.userList: size: 99; requests: 0; hits: --; misses: --
   used.tool.ids: size: 999; requests: 386492; hits: 82.27%; misses: 17.73%
   cms.ContentEntry.localEntries: size: 20640; requests: 38636; hits: 44.64%; misses: 55.36%
   PORTFOLIO_ORG_MAP_CACHE_REGION: size: 0; requests: 0; hits: --; misses: --
   ANNOUNCEMENT.MYWEBCT.CHANNEL.LCTYPES: size: 1; requests: 202696; hits: 100.0%; misses: 0.0%
   ORG_MAP_CACHE_REGION: size: 999; requests: 1218665; hits: 97.1%; misses: 2.9%
   lc.AuthenticationSources: size: 1; requests: 282; hits: 99.65%; misses: 0.35%
 
Database:  Current number of active connections: 7
Database:  High water mark of active connections: 39
Database:  Total number of JDBC connections: 50
Database:  Number of leaked connections: 0
Database:  Averaged time necessary to get a connection (milliseconds): 87
Database:  Current total number waiting for a connection: 0
Database:  High water mark of waiters for a connection: 0
Database:  Number of seconds the longest waiter for a connection waited: 0
 
JMS:  Current connection count to this Server (WebCTManagedNodeJ): 14
JMS:  Peak connection count to this Server (WebCTManagedNodeJ): 15
JMS:  Total connection count to this Server (WebCTManagedNodeJ): 5088
JMS Destination Counts:  current consumers, high consumers, current messages, high messages, pending messages, received messages
 JMS: IntegrationFrameworkJMSServer.InboundQueue:  -1,-1,-1,-1,-1,-1
 JMS: IntegrationFrameworkJMSServer.OutboundQueue:  -1,-1,-1,-1,-1,-1
 JMS: IntegrationFrameworkJMSServer.ErrorQueue:  -1,-1,-1,-1,-1,-1
 JMS: ChatWBJMSServer.ChatWB.RoomTopic:  -1,-1,-1,-1,-1,-1
 JMS: ChatWBJMSServer.ChatWB.RoomTopicLogger:  -1,-1,-1,-1,-1,-1
 JMS: ChatWBJMSServer.WebctEventTopic:  -1,-1,-1,-1,-1,-1
 JMS: ChatWBJMSServer.LicensingIssueQueue:  -1,-1,-1,-1,-1,-1
 JMS: ChatWBJMSServer.MediaLibraryChange:  -1,-1,-1,-1,-1,-1
 
XSLT Pool:  Allocated number of objects: 0,0,0,0,0,0,0,0,0,0
XSLT Pool:  Available (free) number of objects: 10,10,10,11,10,10,10,10,10,10
XSLT Pool:  High water mark of the total number of allocated objects: 2,2,2,2,2,2,2,2,3,2
XSLT Pool:  Low water mark of the total number of available (free) objects: 8,8,8,8,7,8,7,7,7,8
XSLT Pool:  Average allocated objects, per hour: 0,0,1620,1618,1620,0,1832,1832,1832,1830
XSLT Pool:  Average available (free) objects, per hour: 0,0,1620,1618,1620,0,1832,1832,1832,1830
Tracking Queue: Current/Maximum/Total in last window: 0/1/455
 
 
JVM Monitor:  Heap size (bytes): 1610547200
JVM Monitor:  Available memory in heap (bytes): 395916840
 
ChatWB:  No data detected, chat server may not running.
 
Guest Session Pool:  Allocated number of guest sessions: 0
Guest Session Pool:  Available (free) number of guest sessions: 25
Guest Session Pool:  High water mark of the total number of allocated guest sessions: 6
 
Vista Application:  Current number of sockets registered for socket muxing: 1548
Vista Application:  Current number of waiting requests in the self-tuning thread pool: 0
Vista Application:  Total number of threads in the self-tuning thread pool: 28
Vista Application:  Current total number of alive servers in this cluster: 8
]]></log4j:message>
</log4j:event>

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:peterbrowne
ID: 24212286
That should have read WITHOUT duplicating the data entries...  The problem seems to be where the insert query is...
0
 

Author Comment

by:peterbrowne
ID: 24212303
See http://elearning.ucc.usyd.edu.au/cedata/index2.php for the table version using the code just above.
0
 
LVL 9

Expert Comment

by:richdiesal
ID: 24212315
Here's what's happening:
1) Code scans line by line and discovers a timestamp, then inserts a new line into the DB with that timestamp and and empty usercount
2) Code continues scanning and discovers a usercount, then inserts a new line into the DB with that usercount and the timestamp from step #1
3) Code conitnues scanning and discovers a timestamp, then inserts a new line into the DB with that timestamp and the usercount from step #2
4) Repeat from step 2

So it's not double-inserting exactly.  It just looks sort of that way.  

Can you safely assume that a usercount will always follow an associated timestamp?  Then you could use the snippet below.
<?php
require_once('connection/connect.php');
mysql_select_db($database, $connection);
 
//$matches=array('timestamp=', 'Current user count:'); 
 
$log = "webct_monitor.log";
 
$openLog = fopen($log, 'r') or exit('Unable to open file!');  
while(! feof($openLog)) {  
$newline=fgets($openLog);
	if(strstr($newline, $match)){   
		switch ($match){  
			case 'timestamp=':  
				$newline1 = explode($match, $newline);  
				$substring = explode('time=', $newline1[1]); 
				$timeStamp = str_replace('"','',$substring[0]);
				break;  
			case 'Current user count:':  
				$substring = explode($match, $newline);  
				$userCount = $substring[1];  
				$query = "INSERT INTO node_a_data (node_a_data_id,timestamp,user_count) VALUES ('','$timeStamp','$userCount')";
				$result = mysql_query($query, $connection) or die(mysql_error());
				break;  
		} 
	}  
}  
fclose($openLog);  
?>

Open in new window

0
 
LVL 9

Expert Comment

by:richdiesal
ID: 24212327
Sorry, meant this one.
<?php
require_once('connection/connect.php');
mysql_select_db($database, $connection);
 
//$matches=array('timestamp=', 'Current user count:'); 
 
$log = "webct_monitor.log";
 
$openLog = fopen($log, 'r') or exit('Unable to open file!');  
while(!feof($openLog)) {  
	$newline=fgets($openLog);
	if(strstr($newline, 'timestamp=')) {   
		$newline1 = explode($match, $newline);  
		$substring = explode('time=', $newline1[1]); 
		$timeStamp = str_replace('"','',$substring[0]);
	} elseif(strstr($newline, 'Current user count:')) {
		$substring = explode($match, $newline);  
		$userCount = $substring[1];  
		$query = "INSERT INTO node_a_data (node_a_data_id,timestamp,user_count) VALUES ('','$timeStamp','$userCount')";
		$result = mysql_query($query, $connection) or die(mysql_error());
	}  
}  
fclose($openLog);  
?>

Open in new window

0
 

Author Comment

by:peterbrowne
ID: 24212357
Almost there...it is populating the table with 17 entries instead of 33.  However, no data in the timestamp of user count being entered into the table.
0
 

Author Comment

by:peterbrowne
ID: 24212361
What is $match holding then?
0
 
LVL 9

Expert Comment

by:richdiesal
ID: 24212375
Sorry - I posted two sets of code.  The second one doesn't have $match (and should work).
0
 
LVL 9

Accepted Solution

by:
richdiesal earned 2000 total points
ID: 24212382
Try this one.
<?php
require_once('connection/connect.php');
mysql_select_db($database, $connection);
 
//$matches=array('timestamp=', 'Current user count:'); 
 
$log = "webct_monitor.log";
 
$openLog = fopen($log, 'r') or exit('Unable to open file!');  
while(!feof($openLog)) {  
	$newline=fgets($openLog);
	if(strstr($newline, 'timestamp=')) {   
		$newline1 = explode('timestamp=', $newline);  
		$substring = explode('time=', $newline1[1]); 
		$timeStamp = str_replace('"','',$substring[0]);
	} elseif(strstr($newline, 'Current user count:')) {
		$substring = explode('Current user count:', $newline);  
		$userCount = $substring[1];  
		$query = "INSERT INTO node_a_data (node_a_data_id,timestamp,user_count) VALUES ('','$timeStamp','$userCount')";
		$result = mysql_query($query, $connection) or die(mysql_error());
	}  
}  
fclose($openLog);  
?>

Open in new window

0
 

Author Comment

by:peterbrowne
ID: 24212384
This is sort of working: No duplicates, but the first record has '0' current users:

(1, '1240230900533', 0),
(2, '1240231200608', 255),
(3, '1240231500534', 285),
(4, '1240231800550', 319),
(5, '1240232100544', 343),
(6, '1240232400537', 370),
(7, '1240232700545', 390),
(8, '1240233000535', 393),
(9, '1240233300551', 379),
(10, '1240233600558', 359),
(11, '1240233900539', 355),
(12, '1240234200526', 332),
(13, '1240234500527', 312),
(14, '1240234800543', 298),
(15, '1240235100533', 288),
(16, '1240235400537', 293),
(17, '1240235700541', 284),
(18, '1240236000531', 273),
(19, '1240236300535', 264),
(20, '1240236600534', 251),
(21, '1240236900530', 245),
(22, '1240237200536', 224),
(23, '1240237500531', 216),
(24, '1240237800524', 205),
(25, '1240238100516', 194),
(26, '1240238400523', 183),
(27, '1240238700537', 164),
(28, '1240239000531', 151),
(29, '1240239300534', 145),
(30, '1240239600360', 134),
(31, '1240239900355', 124),
(32, '1240240200367', 121),
(33, '1240240500371', 115),
(34, '1240240800371', 113),
(35, '1240241100377', 109),
(36, '1240241400377', 97),
(37, '1240241700408', 97),
(38, '1240242000398', 89),
<?php
require_once('connection/connect.php');
mysql_select_db($database, $connection);
 
$matches=array('timestamp=', 'Current user count:'); 
 
$log = "webct_monitor.log";
 
$openLog = fopen($log, 'r') or exit('Unable to open file!');  
while(!feof($openLog)) {  
$newline=fgets($openLog);
	foreach($matches as $match){  
		if(strstr($newline, $match)){   
			switch ($match){  
			case 'timestamp=':  
			$newline1 = explode($match, $newline);  
			$substring = explode('time=', $newline1[1]); 
			$timeStamp = str_replace('"','',$substring[0]);
			$query = "INSERT INTO node_a_data (node_a_data_id,timestamp,user_count) VALUES ('','$timeStamp','$userCount')";
            $result = mysql_query($query, $connection) or die(mysql_error());
			break;  
			case 'Current user count:':  
			$substring = explode($match, $newline);  
			$userCount = $substring[1];  
			break;  
			} 
		}  
	}  
}  
 
fclose($openLog);  
?>

Open in new window

0
 
LVL 9

Expert Comment

by:richdiesal
ID: 24212401
Did you try the 2:08 AM code?  It should work.
<?php
require_once('connection/connect.php');
mysql_select_db($database, $connection);
 
//$matches=array('timestamp=', 'Current user count:'); 
 
$log = "webct_monitor.log";
 
$openLog = fopen($log, 'r') or exit('Unable to open file!');  
while(!feof($openLog)) {  
	$newline=fgets($openLog);
	if(strstr($newline, 'timestamp=')) {   
		$newline1 = explode('timestamp=', $newline);  
		$substring = explode('time=', $newline1[1]); 
		$timeStamp = str_replace('"','',$substring[0]);
	} elseif(strstr($newline, 'Current user count:')) {
		$substring = explode('Current user count:', $newline);  
		$userCount = $substring[1];  
		$query = "INSERT INTO node_a_data (node_a_data_id,timestamp,user_count) VALUES ('','$timeStamp','$userCount')";
		$result = mysql_query($query, $connection) or die(mysql_error());
	}  
}  
fclose($openLog);  
?>

Open in new window

0
 

Author Closing Comment

by:peterbrowne
ID: 31573653
Great work...works!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question