LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-05-2008, 10:11 AM   #1
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272

Rep: Reputation: 30
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!!
 
Old 11-05-2008, 01:23 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 13,803

Rep: Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365
Quote:
Originally Posted by blizunt7 View Post
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.
 
Old 11-05-2008, 01:36 PM   #3
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272

Original Poster
Rep: Reputation: 30
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?
 
Old 11-05-2008, 03:35 PM   #4
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 13,803

Rep: Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365Reputation: 2365
Quote:
Originally Posted by blizunt7 View Post
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.
 
Old 11-05-2008, 05:15 PM   #5
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272

Original Poster
Rep: Reputation: 30
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?
 
Old 11-05-2008, 07:43 PM   #6
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 3,984

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by blizunt7 View Post
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
 
Old 11-05-2008, 08:58 PM   #7
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272

Original Poster
Rep: Reputation: 30
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?
 
Old 11-06-2008, 05:56 AM   #8
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 3,984

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
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
 
Old 11-06-2008, 06:10 AM   #9
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,089

Rep: Reputation: 207Reputation: 207Reputation: 207
Hi

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

php -r "phpinfo();" |grep .ini
 
Old 11-06-2008, 10:22 AM   #10
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272

Original Poster
Rep: Reputation: 30
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!
 
Old 11-06-2008, 11:12 AM   #11
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,089

Rep: Reputation: 207Reputation: 207Reputation: 207
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.

Last edited by Guttorm; 11-06-2008 at 11:14 AM.
 
Old 11-06-2008, 11:59 AM   #12
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272

Original Poster
Rep: Reputation: 30
Thanks very much for the help!!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
my database export shell script run from command line but not form crontab saifee Linux - General 11 01-29-2012 09:24 AM
running a php script from command line leicaphotos Linux - Newbie 1 10-10-2008 01:24 PM
Apache PHP != Command Line PHP? wh33t Linux - Server 3 10-23-2007 09:21 PM
Problems running PHP script in command line Hondro Linux - Software 1 06-21-2007 04:16 AM
to run a html or php script with php5 command vincent_fr_60 Linux - Software 1 01-20-2006 06:41 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration