how to store SimpleXML parse value into session or Mysql by PHP

4.14 Rating, Out of: 5 Votes: 71 Star2 Stars3 Stars4 Stars5 Stars
Loading ... Loading ...

PHP store SimpleXML value into Session or MYSQL ??

Well While working with Twitter API i face a problem to store SimpleXML parse value into session or mysql. It works fine on that page where you parse value and store in session but doesn’t work on next pages. and it gives error “Warning: session_start() [function.session-start]: Node no longer exists”.

so not go in very beyond, here is the solution to do that very simple way.

for store in mysql

for store in mysql it simple and easy.. not much problem to do that. suppose you parse xml by cURL

$searchRecords = "http://search.twitter.com/search.atom?q=".$q; 
 
$twREC = curl_init();
 
curl_setopt($twREC, CURLOPT_URL, $searchRecords);
curl_setopt($twREC, CURLOPT_RETURNTRANSFER, TRUE);
$twiData = curl_exec($twREC);
$search_Records = new SimpleXMLElement($twiData);
///////////////////////////////////////// here you can add your php codes for store in mysql
 
INSERT INTO table_name (column1, column2, column3,...)
VALUES ($twitData1->value1, $twitData1->value2, $twitData1->value3,...)
 
/////////////////////////////////
curl_close($twREC);

For Session Major problem with SimpleXml

as mysql you can store parse value into mysql so you can fetch that on any page easily. so that easy. but on session. 1st page it works fine with session too. but next page session start giving error

Warning: session_start() [function.session-start]: Node no longer exists”.

if you try to echo session

output will like this

Array ( [id] => SimpleXMLElement Object ())

so here is the solution

$searchRecords = "http://search.twitter.com/search.atom?q=".$q; 
 
$twREC = curl_init();
 
curl_setopt($twREC, CURLOPT_URL, $searchRecords);
curl_setopt($twREC, CURLOPT_RETURNTRANSFER, TRUE);
$twiData = curl_exec($twREC);
$search_Records = new SimpleXMLElement($twiData);
///////////////////////////////////////// store value like this
 
$_SESSION['value1'] = (int)$search_Records->value1; /// if value <span class="dc-title">integer</span>
$_SESSION['value2'] = (string)$search_Records->value2; /// if value string
 
/////////////////////////////////
curl_close($twREC);

so by this you can use session on next pages too.. don’t forget to add session_start();

Enjoy