LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need help with PHP and MySQL: Undefined function... (https://www.linuxquestions.org/questions/programming-9/need-help-with-php-and-mysql-undefined-function-191042/)

Mega Man X 06-08-2004 06:03 AM

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 :p). 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 :).

Mega Man X 06-08-2004 06:29 AM

Oops, almost there :p. 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 :)

Mega Man X 06-08-2004 06:33 AM

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 :(

Mega Man X 08-05-2004 04:19 AM

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.

Longinus 08-06-2004 07:48 AM

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/

Cedrik 08-06-2004 11:35 AM

I am suprised that in SuSE 9.1 Professional, php is compiled without mysql...

Maybe the default database server in this distribution is Postgree ?

Mega Man X 08-06-2004 04:22 PM

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!

andyrewbobb 09-05-2004 01:39 AM

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

Mega Man X 09-05-2004 02:19 AM

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 :)

andyrewbobb 09-05-2004 04:13 PM

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

andyrewbobb 09-13-2004 11:14 PM

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

Mega Man X 09-14-2004 02:20 AM

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...

andyrewbobb 09-15-2004 11:07 PM

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?


All times are GMT -5. The time now is 04:32 PM.