LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   run php script from command line (https://www.linuxquestions.org/questions/programming-9/run-php-script-from-command-line-681307/)

blizunt7 11-05-2008 10:11 AM

run php script from command line
 
Hey all,
I have a PHP script that I want to run from the command line. I am connecting to a mysql database. When i run from the web browser, it connects and runs all queries just fine. When I run from the command line I get the following error:

Code:

Fatal error: Call to undefined function mysql_connect() in /path/to/file.php
Any ideas why i can connect from the web browser, but not from the command line?

Thanks all!!

TB0ne 11-05-2008 01:23 PM

Quote:

Originally Posted by blizunt7 (Post 3332500)
Hey all,
I have a PHP script that I want to run from the command line. I am connecting to a mysql database. When i run from the web browser, it connects and runs all queries just fine. When I run from the command line I get the following error:

Code:

Fatal error: Call to undefined function mysql_connect() in /path/to/file.php
Any ideas why i can connect from the web browser, but not from the command line?

Thanks all!!

Is this the EXACT same PHP file, or do you have web-side includes? Sounds like the database connection isn't getting made, or that the mysql connect is failing. If the web session makes a database connection/authentication earlier in the process, that session ID will be authenticated, and can just read/write.

blizunt7 11-05-2008 01:36 PM

Yes, same file, and yes I am including the connection strings from a separate file:


Code:

include_once "../dbconnect.php";
      $query = "SELECT * FROM $table";
      .....
      .....

mysql_close($database);

Does the connection strings need to be in the file. I cannot include them?

TB0ne 11-05-2008 03:35 PM

Quote:

Originally Posted by blizunt7 (Post 3332662)
Yes, same file, and yes I am including the connection strings from a separate file:

Code:

include_once "../dbconnect.php";
      $query = "SELECT * FROM $table";
      .....
      .....

mysql_close($database);

Does the connection strings need to be in the file. I cannot include them?

The include may be the problem. When your web server offers that up, the path to the page is relative to your docroot, so the "../dbconnect.php" is in a different place. When you go from command-line, the relative path may be different...try to specify the dbconnect.php file with a fully-qualified path, and see what happens.

blizunt7 11-05-2008 05:15 PM

TB0ne,
Thanks for the try, but that did not work. I copied dbconnect.php into the same directory as my script, and changed the include to the relative path (of the local file). I also tried placing the connection string code in my file and that also did not work.

Side note:
Code:

[root@web1 common]$php -m
[PHP Modules]
ctype
date
dom
filter
hash
iconv
json
libxml
pcre
PDO
pdo_sqlite
posix
Reflection
session
SimpleXML
SPL
SQLite
standard
tokenizer
xml
xmlreader
xmlwriter

[Zend Modules]

[root@web1 common]$

I dont see mysql in this list. However, why would it work with apache and not from the command line?

jlinkels 11-05-2008 07:43 PM

Quote:

Originally Posted by blizunt7 (Post 3332835)
I dont see mysql in this list. However, why would it work with apache and not from the command line?

Because PHP for SQL and PHP for CLI have different configurations. Look in /etc/php4/cli/conf.d for mysql.ini and check that the library is loaded.

Compare the contents with /etc/php4/apache/conf.d/mysql.ini. Also check if /etc/php4/cli/php.ini does not prohibit the loading of mysql.

I know I am pointing you to the php4 directories. I AM running php5, so I hope the directory name is a leftover of the old times. I also see that I have edited php.ini at least once, perhaps to correct the same problem as you have. Even if your system is different, the clue might be the different config files.

jlinkels

blizunt7 11-05-2008 08:58 PM

jlinkels,
Interesting information. I didnt know running from browser and CLI use different configuration files. However, I do not have a /etc/php4 OR /etc/php5 directory.
Code:

$ php -v
php 5.2.3

Code:

phpinfo();  # run from browser
Reports php is 4.3.9.
I remember having trouble when I setup php with mysql. PHP 5 functions do not work via web browser, so I know I am running 4.3.9.
I tried search for php, php4, php5 directories, and found nothing.

Any ideas?

jlinkels 11-06-2008 05:56 AM

I just looked it up on a system where I installed PHP5 without upgrading from a previous version. There the directory name is /etc/php5/...

If this is your FC system, I am lost about where FC stores its config files. Unfortunately FC and Debian have some different ideas as where to store configurations. But if it is your Ubuntu system, I am surprised if there is not /etc/phpx directory.

Anyway, I also expect a conf.d somewhere containing a file mysql.ini which contains the name of the loadable module, which is mysql.so.

There are many more conf.d on your system and some more mysql.ini. Use your common sense to check which are applicable. Also, did you try to find the files with 'locate'? Did you do an updatedb?. Did you search with 'find' and use the proper globbing? '"*php*"' or so? (without the SINGLE quotes, but including the double quotes)

jlinkels

Guttorm 11-06-2008 06:10 AM

Hi

phpinfo() shows the .ini files being read. Look at the output of:

php -r "phpinfo();" |grep .ini

blizunt7 11-06-2008 10:22 AM

Guttorm,
Thanks. Found the following:
Code:

[root@web1 josh]$php -r "phpinfo();" | grep .ini
Configuration File (php.ini) Path => /usr/local/lib
Loaded Configuration File => /usr/local/lib/php.ini

in order to load the mysql.so extension, do I need to recompile php? or just restart associated services?

Thanks for all the help guys!

Guttorm 11-06-2008 11:12 AM

It seems you have two different versions of PHP installed, and since you compiled and installed it on "/usr/local/", the easiest thing is probably to compile it again with mysql support.

PHP used to include the mysql client library before, but they dropped it, since it's usually handled better by the distros. So if you had problems installing mysql support in php5, it's probably because you don't have it installed. Your distro should have a package called "libmysqlclient15-dev" or similar.

Also, when you run php from the command line, there are no services involved, so there is nothing to restart.

php -r "phpinfo();" |less

This will show the configuration of the command line version, and until it shows the mysql module, the mysql functions will not work.

blizunt7 11-06-2008 11:59 AM

Thanks very much for the help!!!


All times are GMT -5. The time now is 02:19 PM.