Jump to content

Function call sequence problem?


stevieontario

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.