Caleuanhopkins
Caleuanhopkins

Reputation: 175

Command Line PHP Script not executing correctly

I have a script which I am attempting to execute in the command line using the command on my web server:

php /path_to_file/file.php 'alert' >> /path_to_log_directory/exec.log &

With obviously the two paths pointing at the location of the log and php file. The php file has the following code:

<?php
set_time_limit(3600);

require_once(dirname(dirname(__FILE__)).'/apps/init.php');
include (dirname(dirname(__FILE__)) . '/apps/avl_counter.php');
AvlCounter::$showReports = false;
AvlCounter::updateWholeInventory(false);

?>

Now the updateWholeInventory call a function "mysql_subquery" from the init.php file which is not in a class, the function it's calling is a global function. However, when I attempt the command line PHP command above, the terminal give me:

PHP Fatal error:  Call to undefined function mysql_subquery() in /path_to_file/avl_counter.php on line 521

The file works fine on my local environment which is MAMP running apache but the webserver is running nginx and an older version of PHP. Can anyone help work out why this file executes fine on my local setup but not on my web server?

Here's a cut down version of init.php:

<?

date_default_timezone_set('America/Chicago');
require(dirname(__FILE__) . '/config.php');
if (DEBUG) {
    ini_set('display_errors', true);
    error_reporting(E_ALL ^ E_NOTICE);
} else {
    error_reporting(0);
}
ob_start();
...
function mysql_subquery( $file=false,$line=false,$query=false) { 
    global $queryLog;
    if (empty($queryLog))
        $queryLog = array();
    $start = microtime(true);
    // ...
} 

UPDATE

I am also getting the problem that when the PHP file executed in the shell, the init.php's code is being returned almost as text in the shell rather than being processed as a PHP script. Any ideas?

Upvotes: 0

Views: 5774

Answers (3)

user1233508
user1233508

Reputation:

I am also getting the problem that when the PHP file executed in the shell, the init.php's code is being returned almost as text in the shell rather than being processed as a PHP script.

This sounds like you don't have short tags enabled.

When running through CLI, PHP can be using different php.ini than it does under Apache. Run php -i to determine the config file(s) it is using, then edit the appropriate config file to enable short tags.

Upvotes: 1

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146450

You've configured PHP to hide notices or all error messages, depending on a constant called DEBUG. Please compare:

<?php

error_reporting(E_ALL ^ E_NOTICE);
$foo1++;


error_reporting(0);
$foo2++;


error_reporting(E_ALL);
$foo3++;

Fiddle

Fix this and you'll get a clue about what's going on.

Edit

am also getting the problem that when the PHP file executed in the shell, the init.php's code is being returned almost as text in the shell rather than being processed as a PHP script. Any ideas?

Most likely, you haven't enabled short_open_tag. Of course, this fully explains the fatal error: if the code where you define function mysql_subquery() doesn't run, there's no way to use it :)

Weird thing is that you should have seen the source code pouring down your terminal. I guess that output buffering was masking it.

Upvotes: 0

echo_Me
echo_Me

Reputation: 37233

use this

 mysql_query()

instead of

 mysql_subquery()
  • you should use PDO or mysqli instead of mysql. mysql is already deprecated

Upvotes: 0

Related Questions