views:

385

answers:

3

I wrote a script to read the Netflix catalog and populate my DB. Everything worked fine as a web script (except for timeouts) so I shifted to calling directly form the console.

I noticed a few oddities, like __construct() no longer being called (but that was easily remidied using the class name as a function.

Now I can't get my arrays to work as before, here's the general Idea.

(actually Ihave trioed a few combos, so Ill share them all)

1 - this worked in the web script versino just fine, no longer works calling from console

//declare empty
var $genreArray=array();

//later I add values one at a time as the XML is parsed
array_push($this->genreArray,$attrs['term']);

//after I have parsed an entire "title" element, I iterate the array
foreach ($this->genreArray as $value) {
    // never gets called - array seen as empty
    $this->db->linkGenre($value,$this->title_uid);
}

2 - so I tried the PHP manul; recommendation - nothiong

//declare empty
var $genreArray=array();

//later I add values one at a time as the XML is parsed
$this->genreArray[]=$attrs['term'];

//after I have parsed an entire "title" element, I iterate the array
foreach ($this->genreArray as $value) {
    // never gets called - array seen as empty
    $this->db->linkGenre($value,$this->title_uid);
}

3 - so finally i tried manually tracking the index

//declare empty array
var $genreArray=array();
var $gi=0;

//later I add values one at a time as the XML is parsed
$this->genreArray[$this->gi++]=$attrs['term'];

//after I have parsed an entire "title" element, I iterate the array
foreach ($this->genreArray as $value) {
    // never gets called - array seen as empty
    $this->db->linkGenre($value,$this->title_uid);
}

SO I am totally stumped now.

Has anyone declared empty arrays and populated via console?

(all 3 of these work via the web - so I need a console expert here)


Thanks for the support, here are the additonal details requested;

php -v

PHP 4.4.9 (cli) (built: Sep 17 2008 12:02:18) Copyright (c) 1997-2008 The PHP Group Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies with Zend Extension Manager v1.2.2, Copyright (c) 2003-2007, by Zend Technologies with Zend Optimizer v3.3.3, Copyright (c) 1998-2007, by Zend Technologies


each snippet was tried in a seperate run. what detaisl on the class are you interested in?

I have used echo statements to verify that the code is being called as expected. And if I hit the script through a URL everyhting kicks off fine (for the first few thousand records until the timeout)


No errors are being thrown, I even tried adding

error_reporting(E_ALL);
ini_set('display_errors', true);
+2  A: 

I noticed a few oddities, like __construct() no longer being called (but that was easily remidied using the class name as a function.

It sounds like your command-line and web server PHP are different versions. Is your CLI PHP 4? The function phpversion() will tell you the version in both CLI and web.

Have you checked your error log? You didn't mention it, but that would be the first place to look.

Ben James
Nice catch. But I doubt that is the problem otherwise the array stuff would work, even in PHP 4.
Alix Axel
CLI was php 4, by calling php5 explicitly everything cooked along fine!Thank you!
Eddie
@Eddie: Still the script should work in PHP 4, were you using SimpleXML or any other PHP 5 specific extension?
Alix Axel
+1  A: 

I noticed a few oddities, like __construct() no longer being called (but that was easily remidied using the class name as a function.

This seems weird. What PHP version were you using in the web script and what version in the console?

Typing php -v will tell you the version.

Anti Veeranna
A: 

To avoid the non-CLI timeout you can do something like this:

set_time_limit(0);


You seem to be using var instead of public for the class property declarations, that together with the fact that you can't use __construct() makes me guess you're using PHP 4, is that right?

Where is $attrs['term'] declared?

Try doing var_dump($attrs['term']); to see what comes up.

One other thing that doesn't smell right is the following lines:

array_push($this->genreArray,$attrs['term']);
$this->genreArray[]=$attrs['term'];
$this->genreArray[$this->gi++]=$attrs['term'];

$attrs['term'] seems to be an array, but you aren't looping through it. Unless those lines are in a separate method of it's own I think something is wrong there.

Also, is the code you provided us part of a class? Just to make sure..

Alix Axel
Alix. $attrs is the list of attributes for an xml element, I am only concerned witht he term, so i need not iterate that array. It could just as well be a string "foo", and it was indeed being popualterd correctly by the parser.Anyway Ben was right on, my CLI versino was 4, which doesn't seem to play the way I was used to in m y web version 5.Thanks to all!
Eddie