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
  Search this Thread
Old 06-08-2004, 06:03 AM   #1
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Unhappy Need help with PHP and MySQL: Undefined function...


Hi there!!!

This is the first time that I took PHP and MySQL seriously to learn, but I warn you that I'm still a newbie . Well, some background:

1 - I created a database with MySQL called "mydb". That database has a single table called "workers". (I was using "employees" instead, but I oftenly spell that word wrong, lol ). The table looks like this:

Code:
mysql> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| workers        |
+----------------+
1 row in set (0.00 sec)
Code:
mysql> show columns from workers;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | tinyint(4)   |      | PRI | NULL    | auto_increment |
| first    | varchar(20)  | YES  |     | NULL    |                |
| last     | varchar(20)  | YES  |     | NULL    |                |
| adress   | varchar(255) | YES  |     | NULL    |                |
| position | varchar(50)  | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
Code:
mysql> select * from workers;                                                   +----+-------+---------+-----------------------------+-------------------+
| id | first | last    | adress                      | position          |
+----+-------+---------+-----------------------------+-------------------+
|  1 | Bob   | Smith   | 128 Here St, Citnmame       | Marketing Manager |
|  2 | John  | Roberts | 45 There St, Townville      | Telephonist       |
|  3 | Brad  | Johnson | 1/34 Nowhere Blvd,
Snowston | Doorman           |
+----+-------+---------+-----------------------------+-------------------+
3 rows in set (0.00 sec)
As you can see, everything seems to be working with the database. Now to the PHP code trying to connect to the database:

PHP Code:
<html>
<head>
<title></title>
</head>

<body>

  <?php
  
  $db 
mysql_connect("localhost""root");
  
mysql_select_db("mydb",$db);
  
  
$result mysql_query("SELECT * FROM workers",$db);
  
printf("First Name: %s<br>\n"mysql_result($result,0,"first"));
  
printf("Last Name: %s<br>\n"mysql_result($result,0,"last"));
  
printf("Address: %s<br>\n"mysql_result($result,0,"address"));
  
printf("Position: %s<br>\n"mysql_result($result,0,"position"));
  
  
?>

  
</body>
</html>
Whenever I try to open my page, (Apache runs fine and has support for both PHP and MySQL, according to "PHPinfo()") I get this error:

Quote:
Fatal error: Call to undefined function: mysql_connect() in /var/www/html/index.php on line 10
Question 1
That would be the line: $db = mysql_connect("localhost", "root");. I know two problems in there: First, I've to specify my root password right? How? . Second, is Localhost correct? Or should it be my IP Address or something?

Question 2
Now another stupid question.... Where is "mydb" located? I've tried and tried and tried to locate that file, but I could not. This is Mandrake 9.2 if that's of any help.

Thanks a lot for any help, hint or guess in advance .

Last edited by Mega Man X; 06-08-2004 at 06:08 AM.
 
Old 06-08-2004, 06:29 AM   #2
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Original Poster
Rep: Reputation: 65
Oops, almost there . It turns out that I've forgotten to install a package, called "php-mysql". That seems to be the one responsable to connect php code to mysql. Running my page I now get:

Quote:
First Name: Bob
Last Name: Smith

Warning: mysql_result(): address not found in MySQL result index 3 in /var/www/html/index.php on line 16
Address:
Position: Marketing Manager
A little better, I guess
 
Old 06-08-2004, 06:33 AM   #3
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Original Poster
Rep: Reputation: 65
Fixed:

Quote:
First Name: Bob
Last Name: Smith
Address: 128 Here St, Citnmame
Position: Marketing Manager
It was a little mistake. I wrote "adress" at the table "workers", and it should be "address"... I've to learn how to spell, I really do
 
Old 08-05-2004, 04:19 AM   #4
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Original Poster
Rep: Reputation: 65
Ok, I need help with this again. I've recently switched to SuSE 9.1 Professional and, while still trying to learn PHP/MySQL (I will finish it someday, I promise) the very same error as the above has pop up again.

For my surprise, I have php4-mysql package installed. phpinfo() shows "--without-mysql", so that's where the problem is, I guess.

I've figured, it's just a matter to go into /etc/apache2/http.config and uncomment the php4 line. For my astonishment, they've "broke" the hole file, into small parts and used "Include" to add them. No problems there, it looks even more organized. Problem:

I cannot find anywhere on any of the Include files a line to enable PHP4 into Apache2's config. Is there anyone out there using php4/apache2/mysql and preferenclibly with SuSE to guide me in this big dark maze of files and configurations?

PS: I do, have PHP4, Apache and MySQL running. Just cannot connect php to mysql.

Thanks in advance.
 
Old 08-06-2004, 07:48 AM   #5
Longinus
Member
 
Registered: Sep 2003
Distribution: Redhat 9.0 && Slackware 9.1
Posts: 420

Rep: Reputation: 30
wait, you have PHP running?

why do you want to find the thing in the httpd.conf file then?

EDIT:

oh oops,
you dont need to be looking in the apache stuff

you need to recompile php and when you config it make sure to add --with-mysql=/path/to/mysql/

Last edited by Longinus; 08-06-2004 at 07:50 AM.
 
Old 08-06-2004, 11:35 AM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
I am suprised that in SuSE 9.1 Professional, php is compiled without mysql...

Maybe the default database server in this distribution is Postgree ?
 
Old 08-06-2004, 04:22 PM   #7
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Original Poster
Rep: Reputation: 65
Thanks Longinus and Cedrik for you help!

I did a big mistake in my post. I mean to find a line in Apache's config to enable mysql, since php does work.

Well, I wanted to remove all the packages and compile Apache, PHP and mysql myself, but I have had bad experiences with that in Slack 9.0, and compiling things in SuSE is not very pleasent .

I fireup YaST and they removed all packages related to apache/php/mysql and installed "Simple webserver with Apache2", a collection of packages including mysql, php and apache2.

It's now working perfectly, I guess

Thanks for your tips my friends!
 
Old 09-05-2004, 01:39 AM   #8
andyrewbobb
LQ Newbie
 
Registered: Jan 2004
Location: MO
Distribution: Suse 9.1 Pro
Posts: 11

Rep: Reputation: 0
prob w/mysql

hia, im a linux newbie, and im using 9.1 also, and am having the exact same problem, except i dont know how to recomple anything...so what i need is somehow to either do it w/o recompling it, or for someone to give me a detailed step by step on how to recomple php. thank you
 
Old 09-05-2004, 02:19 AM   #9
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Original Poster
Rep: Reputation: 65
Hi there andyrewbobb!

Welcome . Try doing what I did, remove all package through YaST and choose the "Simple Web Server with Apache2" in YaST. Then, make sure that both apache and mysql are running:

rcapache2 start
rcmysql start

You can just as well start those server under YaST's Runlevel Editor. If the problem persists, post some code here and the exact same error message so we can take a look. I'm very, very bad with PHP, but we never know
 
Old 09-05-2004, 04:13 PM   #10
andyrewbobb
LQ Newbie
 
Registered: Jan 2004
Location: MO
Distribution: Suse 9.1 Pro
Posts: 11

Rep: Reputation: 0
ok, so when i try to remove all the packages, some of them wont uninstall, i dont know why, i go into the run level editor, and stop apache and mysql, and then uninstall everything that has either apache, php, or mysql in it, and then restart it, and a few of them are still there, so i dunno what im doin wrong

Also, where did you go to get this simple web server with apache2? i searched for it in the package manager, and in the main menu, im lost on that one.

Is there some code i can put into a file that will make php/apache recognize where my mysql is? that would be the best...

ok thank you

Last edited by andyrewbobb; 09-05-2004 at 04:15 PM.
 
Old 09-13-2004, 11:14 PM   #11
andyrewbobb
LQ Newbie
 
Registered: Jan 2004
Location: MO
Distribution: Suse 9.1 Pro
Posts: 11

Rep: Reputation: 0
Ok, so after some searching. i found the simple web server w/apache 2 thing, thanks for that, but i still have the same problem, it wont work w/mysql....i dunno why, phpMyAdmin says mysql is running, and php is running cause i can load the page, but it says that it is --without mysql but in the php.ini file...

[extension section]
extension=snmp.so
extension=ldap.so
extension=pgsql.so
extension=dba.so
extension=zlib.so
extension=yp.so
extension=xslt.so
extension=wddx.so
extension=unixODBC.so
extension=sysvshm.so
extension=sysvsem.so
extension=swf.so
extension=sockets.so
extension=shmop.so
extension=session.so
extension=mcal.so
extension=mysql.so
extension=mime_magic.so
extension=mhash.so
extension=mcrypt.so
extension=gmp.so
extension=curl.so
extension=ctype.so
extension=calendar.so
extension=bz2.so
extension=recode.so
extension=readline.so
extension=qtdom.so
extension=mbstring.so
extension=imap.so
extension=iconv.so
extension=bcmath.so
extension=gettext.so
extension=gd.so
extension=ftp.so
extension=filepro.so
extension=exif.so
extension=domxml.so
extension=dbase.so
; end of extension section

so its deff in there... so i dunno, ne help would be awesome
thanks
 
Old 09-14-2004, 02:20 AM   #12
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Original Poster
Rep: Reputation: 65
Strange... try to initialize the services manually. Open a terminal then:

rcapache2 start
rcmysql start

To make sure they are running. If they are, and you are still getting the same error message, try posting everything as I just did: Page's code, PHP and database tables. I had a problem once that was nothing but a typo...
 
Old 09-15-2004, 11:07 PM   #13
andyrewbobb
LQ Newbie
 
Registered: Jan 2004
Location: MO
Distribution: Suse 9.1 Pro
Posts: 11

Rep: Reputation: 0
ok heres what i know so far, phpmyadmin is finding mysql, and php, but php is not finding mysql....
the info.php page says this about mysql....

mysql
MySQL Support enabled
Active Persistent Links 0
Active Links 0
Client API version 4.0.18
MYSQL_MODULE_TYPE none
MYSQL_SOCKET /var/lib/mysql/mysql.sock
MYSQL_INCLUDE no value
MYSQL_LIBS no value

Directive Local Value Master Value
mysql.allow_persistent On On
mysql.connect_timeout 60 60
mysql.default_host no value no value
mysql.default_password no value no value
mysql.default_port no value no value
mysql.default_socket no value no value
mysql.default_user no value no value
mysql.max_links Unlimited Unlimited
mysql.max_persistent Unlimited Unlimited
mysql.trace_mode Off Off

and so apparently it can find the socket, but what else it needs i dont know...
mysql folder is /var/lib/mysql/ , and that folder contains...
file:/var/lib/mysql/mysql
file:/var/lib/mysql/test
file:/var/lib/mysql/mysqld.log
file:/var/lib/mysql/mysqld.log-20040913.gz
file:/var/lib/mysql/mysqld.log-20040915.gz
file:/var/lib/mysql/mysqld.pid
file:/var/lib/mysql/mysql.sock

and then the mysql folder contains some more files...
what do i need to do to php to get it to recognize the rest of the files? or where do i need to move the rest of the files to get it to be recognized by php?
or is there some simplier way to do it?
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Call to undefined function imagettfbbox() QtCoder Programming 24 01-18-2012 12:20 PM
PHP: call to undefined function preg_match() (I think to do with PCRE) BuckRogers01 Linux - Software 1 10-17-2005 01:35 PM
Fatal Error - Call to undefined function : pg_pconnect() in filename.php on line xx mwpeng Linux - Newbie 1 02-26-2005 08:57 AM
Call to undefined function: mail() in PHP pembo13 Fedora 0 07-28-2004 07:40 PM
PHP: Call to undefined function: session_start() wellmt Linux - Newbie 2 06-01-2004 05:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:03 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration