Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error running php file by command line 1

Status
Not open for further replies.

katie4335

Programmer
Jun 23, 2014
17
GB
Hi,

i'm kind of new to linux and running scripts directly from the command line

my server version is Ubuntu 14.04. (my php version is 5.5.9). I was trying to set up a cron job to run a php script, but was having no success. so i decided to see if the script would just run via the command line (runs fine using a browser).

the error i'm getting is
PHP Fatal error: Call to a member function query() on a non-object in /var/ on line 503

what the error is trying to say… my mysql query is failing. the code on line 503 is
Code:
 $stmt = $db->query($SQL);

not sure why it would fail by running it via command line, whereas it runs successfully via a browser. where am i going wrong, or what tweaks do i need to make and where?


here's a little bit more code for some context….

Code:
line 503	$stmt = $db->query($SQL);
line 504	while($col = $stmt->fetch(PDO::FETCH_ASSOC)) {
line 505	etc, etc…
 
Hi

The key is in "Call to a member function query() on a non-object". So $db is not an object, most likely it is [tt]null[/tt].

So for debugging see the part of your code where you are instantiating that object.

I bet the problem is with connection parameters loaded from a configuration file specified with a relative path. So when you are running it from a different directory the configuration is not found and without the configuration data the database connection object's instantiation fails.

By the way, this question would probably get better answers in forum434.


Feherke.
feherke.ga
 
hey feherke, yes you're right, i'm starting to narrow it down to my include file thats referencing the db connection params...

original include causing the error...
Code:
<?php //include('../template/template_includes.php')?>

did a quick update to this, but didn't work
Code:
<?php include(dirname(__FILE__) . '../template/template_includes.php')?>
 
Hi

[tt]dirname()[/tt] returns the path without trailing separator. Add it manually :
PHP:
<?php include(dirname(__FILE__) . '[COLOR=red yellow]/[/color]../template/template_includes.php');

Feherke.
feherke.ga
 
hi feherke,

here's my code,

Code:
include(''.$_SERVER['DOCUMENT_ROOT'] . '/template/template_includes.php');

works fine via the browser, but i get errors when running via the command line. here's the error message,

PHP Warning: include(/template/template_includes.php): failed to open stream: No such file or directory in /var/ on line 12
 
what i don't get is when i echo $_SERVER['DOCUMENT_ROOT'] outside of the include it gives me, DOCUMENT_ROOT: /var/
but inside use it inside the include, all i'm getting is... PHP Warning: include(/template/template_includes.php): failed
 
Hi

[tt]$_SERVER['DOCUMENT_ROOT'][/tt] has no meaning outside the web server's environment. It is undefined when you run PHP from the command line. ( Think about, if you have multiple web servers installed on your machine, then from which one's settings should the [tt]DOCUMENT_ROOT[/tt] be taken, as long as the command line has nothing to do with any of the servers ? )

Feherke.
feherke.ga
 
ok, i'm racking my brain how to get around this, my problem is

- my local is running Apache/2.4.6 (Ubuntu)...(/var/www)
- and my server is running Apache/2.4.7 (Ubuntu)...(/var/
in the latest apache2 they've created a new 'html' folder where to place web files. so when flipping between server's i'd need to do something to factor in the 'html' directory. so far, all i can come up with is...

1. better learn python, so i'm no longer browser dependent!

2. update my local to Apache/2.4.7 so i can do a full reference of just /var/
any more alternatives?
 
Hi

katie4335 said:
1. better learn python, so i'm no longer browser dependent!
Makes no difference. Your problem comes from the environment differences and they will be similar regardless the used language.

katie4335 said:
2. update my local to Apache/2.4.7 so i can do a full reference of just /var/www/html/template/template_includes.php
Keeping server softwares up to date is a basic requirement if you aim security. However, just to solve a path issue is an overkill.

katie4335 said:
any more alternatives?
First of all, why not use [tt]dirname(__FILE__)[/tt] as you did it yesterday ? That looks simple and correct.

Usually when a script has to work in different environments, one handy solution is to use different configuration files. In your case this may be reduced to optionally passing the configuration file name with full path as command line parameter.

Alternatively you can enumerate all the possible places where a configuration file may be found, then loop over the array, check each's presence with [tt]file_exists()[/tt] and [tt]include[/tt] the first found one.


Feherke.
feherke.ga
 
i'm in the directory... /var/
the file i'm trying to reference is inside... /var/
dirname(__FILE__) gives me /var/ ...i need to go back a directory to reference, /var/
i guess i could do an if statement upon recognizing what server it is, but that will also give me a lot of work
 
Hi

And what about Apache's way ? Look in each directory in the branch : /var/ /var/ /var/www, /var, /.

PHP:
$dir = dirname(__FILE__);

[b]while[/b] (true) {
    [b]if[/b] (file_exists([green][i]"$dir/template/template_includes.php"[/i][/green])) {
        [b]include_once[/b] [green][i]"$dir/template/template_includes.php"[/i][/green];
        [b]break[/b];
    }

    [b]if[/b] ($dir == [green][i]'/'[/i][/green])
        [b]break[/b];

    $dir = dirname($dir);
}

Feherke.
feherke.ga
 
thanks feherke, just now rebuilt the server on my local (for some reason i couldn't just update from or get rid of apache2 2.4.6. this was moments before you'd replied. i know i was a bit of a hot head, but if i do need to go back to my previous local config, i'll factor in your suggestion above. i'm using virtualbox so the previous config is still there!

guess i was also getting annoyed that i've been getting immersed in my local then to find a some things didn't work on the server.. real trivial stuff too!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top