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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-05-2008, 10:11 AM
|
#1
|
|
Member
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272
Rep:
|
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!!
|
|
|
|
11-05-2008, 01:23 PM
|
#2
|
|
Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 11,805
|
Quote:
Originally Posted by blizunt7
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.
|
|
|
|
11-05-2008, 01:36 PM
|
#3
|
|
Member
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272
Original Poster
Rep:
|
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?
|
|
|
|
11-05-2008, 03:35 PM
|
#4
|
|
Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 11,805
|
Quote:
Originally Posted by blizunt7
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.
|
|
|
|
11-05-2008, 05:15 PM
|
#5
|
|
Member
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272
Original Poster
Rep:
|
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?
|
|
|
|
11-05-2008, 07:43 PM
|
#6
|
|
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Etch/Lenny/Squeeze
Posts: 3,797
|
Quote:
Originally Posted by blizunt7
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
|
|
|
|
11-05-2008, 08:58 PM
|
#7
|
|
Member
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272
Original Poster
Rep:
|
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:
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?
|
|
|
|
11-06-2008, 05:56 AM
|
#8
|
|
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Etch/Lenny/Squeeze
Posts: 3,797
|
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
|
|
|
|
11-06-2008, 06:10 AM
|
#9
|
|
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,007
Rep: 
|
Hi
phpinfo() shows the .ini files being read. Look at the output of:
php -r "phpinfo();" |grep .ini
|
|
|
|
11-06-2008, 10:22 AM
|
#10
|
|
Member
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272
Original Poster
Rep:
|
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!
|
|
|
|
11-06-2008, 11:12 AM
|
#11
|
|
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,007
Rep: 
|
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.
|
|
|
|
11-06-2008, 11:59 AM
|
#12
|
|
Member
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272
Original Poster
Rep:
|
Thanks very much for the help!!!
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:27 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|