I have migrated all my scripts from mysqli to pdo, for the most part the transition has been smooth.
But there is one thing I can’t make sense of. I have a few scripts that run as cron jobs, once a day. None of them work since changing to pdo. Using Mysqli they work fine.
Initially I was getting this from the connection include:-
PHP Parse error: syntax error, unexpected '[' in /...
Which relates to this line which sets the default fetch.
Running some tests on dummy scripts, I get the same. If I comment out that line, I get nothing, no result from the script or error logged.
Exactly the same connection include is used for other scripts on the actual site, which work just fine.
The only thing I can think of, where the cron jobs differ from scripts on the site is their location. The cron jobs are not in the public_html folder, since they are not for public use.
I could just revert to the original Mysqli scripts, which I may do while I find a fix, but I’d like to know why they don’t work.
EDIT
It does seem to be working now, on the dummy script with the default fetch line disabled.
This kind of syntax errors suggests that you may be running on an older PHP version, which doesn’t support the bracketed syntax for arrays (<=5.3). Cron jobs are usually run from a different environment that the web server so it is possible that there are multiple PHP versions installed and the cron is invoking a different version than your web server so I would first check if the PHP version is the same for both.
That would explain it. The Control Panel says in the stats that it’s on PHP 5.4.24
But in PHP Config from the CP, it only gives a choice between 5.4 and 5.6
Of course I have chosen the newer one. But CP still thinks it’s on 5.4
So it seems stuff that’s not in the public_html is on the older version.
I was wondering the same thing. The instructions I was given by my ISP were to run the cron job from /usr/local/php5.3/bin/php although my websites run 5.6
Well the dummy script is working now, sending me an Email every 5 mins. That’s with that line commented out.
I just changed it to use a prep-statement and execute using brackets to see if it breaks.
The sad thing is, I just re-newed the account this week. I was thinking of moving elsewhere this year, but did not get around to it.
The actual site (in the public_html) is on 5.6.17.
I have a single daily cron job which from time to time fails to run. I have reported it each time to my host but they investigate and give me a bit of bull till the next time. Frustrating doesn’t begin to describe it.
There’s still hope depending on how your host allows you to configure cron jobs. If you are allowed to edit the plain cron entry then you don’t have to invoke your php script via CLI - instead you can run a linux program that will simply load your script’s URL and in this way it will be run on 5.6.17 - exactly in the same environment as from a browser. You can use wget or lynx command,. On one of my servers I have this command for cron:
One script has no prepared statements, so I got it working. But the other does, so I reverted to the old Mysqli version of it for now.
So I think Lemon Juice is right, it’s an old version of php, because the connect include and prepared statements work on the main site scripts.
Sorry, no, I meant in the crontab -e, which should resemble * * * * * /bin/php myscript.php, I’m wondering if we can alter the /bin/php part so it uses a more up-to-date version of PHP.
Prepare statements and PDO shouldn’t be affected by the PHP version - both mySQLi and PDO (both of which support prepare statements) were both introduced in July 2004 in PHP 5.0.
The problem you appear to be having is the square bracket array notation that was introduced a lot more recently. That can be fixed by reverting back to the older (but still supported) way of defining arrays.