bluefrog Posted April 20, 2010 Share Posted April 20, 2010 Just taken over a backend development (dhtml) and the previous coder was using strings in the DB and pulling from php, however they deleted all of their work before I took over and now I don't have much clue how they were pulling the data from the string: a:3:{i:0;a:2:{s:6:"visits";a:5:{i:0;a:1:{i:0;a:2:{s:5:"total";i:59;s:6:"unique";i:53;}}i:1;a:48:{i:1268258400;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1268312400;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1268402400;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1268406000;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1268413200;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1268463600;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1268686800;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1268737200;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1268755200;a:2:{s:5:"total";i:2;s:6:"unique";i:2;}i:1268938800;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1269064800;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1269075600;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1269374400;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1269432000;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1269662400;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1269716400;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1269936000;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1269972000;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270004400;a:2:{s:5:"total";i:2;s:6:"unique";i:1;}i:1270040400;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270072800;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270076400;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270137600;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270152000;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270206000;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270216800;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270288800;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270461600;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270497600;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270558800;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270573200;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270576800;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270926000;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270990800;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271062800;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271084400;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271181600;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271268000;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271318400;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271329200;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271347200;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271412000;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271498400;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271502000;a:2:{s:5:"total";i:2;s:6:"unique";i:2;}i:1271523600;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271530800;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271620800;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271732400;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}}i:2;a:14:{i:1270166400;a:2:{s:5:"total";i:2;s:6:"unique";i:2;}i:1270252800;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270425600;a:2:{s:5:"total";i:2;s:6:"unique";i:2;}i:1270512000;a:2:{s:5:"total";i:3;s:6:"unique";i:3;}i:1270857600;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1270944000;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271030400;a:2:{s:5:"total";i:2;s:6:"unique";i:2;}i:1271116800;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271203200;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271289600;a:2:{s:5:"total";i:3;s:6:"unique";i:3;}i:1271376000;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271462400;a:2:{s:5:"total";i:5;s:6:"unique";i:5;}i:1271548800;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}i:1271721600;a:2:{s:5:"total";i:1;s:6:"unique";i:1;}}i:3;a:8:{i:1267315200;a:2:{s:5:"total";i:7;s:6:"unique";i:7;}i:1267920000;a:2:{s:5:"total";i:7;s:6:"unique";i:7;}i:1268524800;a:2:{s:5:"total";i:7;s:6:"unique";i:7;}i:1269129600;a:2:{s:5:"total";i:4;s:6:"unique";i:4;}i:1269734400;a:2:{s:5:"total";i:12;s:6:"unique";i:11;}i:1270339200;a:2:{s:5:"total";i:6;s:6:"unique";i:6;}i:1270944000;a:2:{s:5:"total";i:14;s:6:"unique";i:13;}i:1271548800;a:2:{s:5:"total";i:2;s:6:"unique";i:2;}}i:4;a:2:{i:1267401600;a:2:{s:5:"total";i:32;s:6:"unique";i:30;}i:1270080000;a:2:{s:5:"total";i:27;s:6:"unique";i:26;}}}s:7:"watched";a:0:{}}i:1;a:0:{}i:2;a:0:{}} That is the data held in the 'data' field of the table, looks like they were trying to track something like visitors. I need some idea's how to pull say the Visits and uniques from the data. Example {s:5:"total";i:59;s:6:"unique";i:53;}}i:1;a:48:{i:1268258400;a:2: would become Total 59, unique 53. Anybody have any idea's they can share? An example would be great (I'm more a visual learner). Quote Link to comment https://forums.phpfreaks.com/topic/199176-totally-confused-pull-data-from-string/ Share on other sites More sharing options...
trq Posted April 21, 2010 Share Posted April 21, 2010 That is a serialized array. You can get the data back into an array by using unserialize. Quote Link to comment https://forums.phpfreaks.com/topic/199176-totally-confused-pull-data-from-string/#findComment-1045514 Share on other sites More sharing options...
bluefrog Posted April 21, 2010 Author Share Posted April 21, 2010 Ah! Makes sense. I've had a read of the page you graciously provided and had a little fumble with it (php is not my thing!) and I'm not getting any output. I'm sure it's something ridiculously simple. Here is the code I'm using: <?php $dbhost = 'localhost'; $dbuser = 'my_user'; $dbpass = 'my_pass'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'my_db'; mysql_select_db($dbname); $query = "SELECT `column` FROM `table`"; function safeUnserialize($serialized) { $serialized = stripslashes($serialized); $unserialized = @unserialize($serialized); if ($unserialized === false) { $serialized = preg_replace('/s:\d+:"([^"]*)";/e', "'s:'.strlen('\\1').':\"\\1\";'", $serialized); $unserialized = @unserialize(stripslashes($serialized)); } return $unserialized; } print($unserialized); ?> All I get is a blank screen as can be seen here: http://www.skinangels.co.uk/test.php Any further idea's? Quote Link to comment https://forums.phpfreaks.com/topic/199176-totally-confused-pull-data-from-string/#findComment-1045737 Share on other sites More sharing options...
trq Posted April 21, 2010 Share Posted April 21, 2010 You need to actually execute your query using mysql_query then fetch the results from the result resource using something like mysql_fetch_assoc. Quote Link to comment https://forums.phpfreaks.com/topic/199176-totally-confused-pull-data-from-string/#findComment-1046156 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.