stevieontario Posted February 20, 2010 Share Posted February 20, 2010 Morning Freaks, I'm trying to recreate code which includes a call to a function in an include file. The original code worked, but the recreated code is giving me problems. I get the following error message Warning: get_object_vars() expects parameter 1 to be object, string given in C:\Program Files\xampp\htdocs\dev\Functions1.php on line 20 Also, the database insert query doesn't put anything into the database. I haven't altered the include file at all. This leads me to believe that my problem is with the main file. Here's the main file code: require_once ("Functions1.php"); ////////////////////////////////////////////////////////// $lastxml = basename('c:/program files/xampp/htdocs/dev/xmlfilename.xml'); $output= simplexml_load_file($lastxml); $rr = object2array($output); $fdate = getdatafromfilename($lastxml); $fversion = getdatafromfilename($lastxml,1); $generated_date = trim($rr['IMODocHeader']['CreatedAt']); $generated_date = str_replace("T"," ",$generated_date); /////////// Database Settings ////////////// $db_hostname = "localhost"; $db_name = "newstats"; $db_username = "steveaplin"; $db_password = "nafta"; $cxn = mysql_connect($db_hostname, $db_username, $db_password); ///////////////////////////////////////////// $insert2sourceinfo2 = "insert into sourceinfo2 (xmlname, filedate, version, generated) VALUES ('$lastxml', '$fdate', '$fversion', '$generated_date')"; mysql_query($insert2sourceinfo2, $cxn); $lastSid = mysql_insert_id(); And here's the include file code: // converts the xml file into a php object; function object2array($object) { $return = NULL; if(is_array($object)) { foreach($object as $key => $value) $return[$key] = object2array($value); } else { $var = get_object_vars($object); if($var) { foreach($var as $key => $value) $return[$key] = object2array($value); } else return strval($object); } return $return; } Line 20 is the following: $var = get_object_vars($object); Anybody have any ideas? Thanks in advance! Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 20, 2010 Share Posted February 20, 2010 What is $object? Where does it come from? Show the relevant code above the point you require the function. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
stevieontario Posted February 20, 2010 Author Share Posted February 20, 2010 thanks teamatomic, That's just it -- the code that I displayed is all the code. $object appears only in the function object2array. I have never touched it before (someone else wrote it), and it worked previously. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 20, 2010 Share Posted February 20, 2010 OK. What it is expecting is this $lastxml = basename('c:/program files/xampp/htdocs/dev/xmlfilename.xml'); but basename returns this: xmlfilename.xml try: $lastxml = 'c:/program files/xampp/htdocs/dev/xmlfilename.xml'; and make sure the file exists HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
stevieontario Posted February 20, 2010 Author Share Posted February 20, 2010 team, thanks again. I should have mentioned that I tested $lastxml using echo var_dump($lastxml); and it printed fine, so I think the problem resides elsewhere. I also echoed $fdate and $fversion (the function getdatafromfilename also resides in "functions1.php"), and that was successful too. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 20, 2010 Share Posted February 20, 2010 did var_dump output an array? HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
stevieontario Posted February 20, 2010 Author Share Posted February 20, 2010 yes, an array of xml elements, which I am able to loop through successfully (that code is below the sql query in the main program). Just to narrow down the problem, I put echo statements inside the looping scripts, and lauched the script to my browser. The echo statements return the expected output. I turn $lastxml (which is an xml file) into an object using simplexml_load_file, then the function object2array turns that object into an array. Or at least it's supposed to. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 20, 2010 Share Posted February 20, 2010 Yes, it works. I put it into RapidPHP and it does what it should. $output is a simplexml object and $rr is an array. Thats why I'm questioning about your xml file. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 21, 2010 Share Posted February 21, 2010 What does adding the following after the line with the simplexml_load_file() statement show - var_dump($output); Quote Link to comment Share on other sites More sharing options...
stevieontario Posted February 21, 2010 Author Share Posted February 21, 2010 var_dump($output) shows the structure of the xml file "xmlfilename.xml": SimpleXMLElement Object ( [@attributes] => Array ( [docID] => etc. The file is fairly large and complex, so I won't show the whole thing Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 21, 2010 Share Posted February 21, 2010 If you have an object before the call to object2array() but code inside the function reports an error about not receiving an object, it is likely that part of your code that is not being shown here is calling that function a second time with non-existent data or the code you have been posting is not the actual code running on the server that is generating the error message. Quote Link to comment Share on other sites More sharing options...
stevieontario Posted February 22, 2010 Author Share Posted February 22, 2010 thanks PFM. This is a mystery. The variable $object doesn't appear anywhere else in "Functions1.php" or in the main file. Also, the function object2array is not called anywhere else, and the code I displayed in the main file is all the code there is that is relevant to this function (from there on down it's just a bunch of if and foreach loops, which all produce the right output). Here's another interesting thing: I finally figured out how to do the $insert2sourceinfo2 query properly, and the variable $generated_date, which is based on $rr, gives the right output! I still get the same warning, but it produces the right output. Quote Link to comment 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.