...

View Full Version : mysql_connect()



applefool
03-11-2003, 04:29 PM
I'm new to PHP/MySQL, and hence have a new setup on RH8. I can't seem to get any php scripts to connect to mysql. I have tried a very basic one that just outputs a db .. and an example program that querys a db in html. with an input form.. Is there something else i needed to set up ?


<html>
<body>
<?php

$db = mysql_connect("localhost", "user", "pass");

mysql_select_db("fuji",$db);

$result = mysql_query("SELECT * FROM people",$db);

printf("Name: %s<br>\n", mysql_result($result,0,"name"));
printf("Title: %s<br>\n", mysql_result($result,0,"title"));
printf("Email: %s<br>\n", mysql_result($result,0,"email"));
printf("AIM: %s<br>\n", mysql_result($result,0,"aim"));
printf("Cell: %s<br>\n", mysql_result($result,0,"cell"));

php?>

</body>
</html>

This is the error this generates:

Fatal error: Call to undefined function: mysql_connect() in /var/www/html/PHP/fuji.php on line 5


Any ideas?

whitty
03-11-2003, 04:34 PM
<?php

$db = mysql_connect("localhost", "user", "pass");

mysql_select_db("fuji");

$result = mysql_query("SELECT * FROM people");

php?>


I _think_ that should do it though I'm not positive.

applefool
03-11-2003, 04:51 PM
I tried that bit of code - it gives me the same error. I'm guessing there is something I have not setup for the php to communicate with the mysql db.

firepages
03-11-2003, 05:17 PM
is that code from webmonkey tutorial ?

, anyway check to where its all going wrong by using the built in functions...



<?php
$db = mysql_connect("localhost", "user", "pass")or die(mysql_error());

mysql_select_db("fuji")or die(mysql_error());

$result = mysql_query("SELECT * FROM people");
if(@mysql_num_rows($result)){//was any data returned ?//
$r=mysql_fetch_array($result);
echo "name=".$r['name'];
echo "<br />email=".$r['email'];
//etc
}else{
echo 'empty result set!';
}
?>


you dont need to call die() if you dont want to , just echo mysql_error().

applefool
03-11-2003, 06:04 PM
No matter what i do, including the last supplied bit, I get this -

Fatal error: Call to undefined function: mysql_connect() in /var/www/html/test2.php on line 5

x_goose_x
03-11-2003, 06:27 PM
Did you start your sql server before or after your web server? mySQL needs to be loaded first.

Spookster
03-11-2003, 06:27 PM
Change:

php?>

to

?>

firepages
03-11-2003, 06:27 PM
ok, then your host has disabled mysql support in your PHP configuration/build & without that you are up the spout I am afraid.

to be sure, run ...
<?php phpinfo();?>
(just save that as phpinfo.php)

and look for mysql in the generated table, but I think thats your problem.


lol - 3 posts in the same minute - now thats service :D

firepages
03-11-2003, 06:33 PM
doh the version I posted was a bit dodgy ... you dont need the

'$db = ' bit on the connect line , not that is the problem here,

Spooks I found out just the other day by accident that



<?php
echo 'whoot';
php?>


does not actually cause an error or even a warning on 4.3.0 , not sure about previous versions though ?

Spookster
03-11-2003, 06:40 PM
Originally posted by firepages

Spooks I found out just the other day by accident that



<?php
echo 'whoot';
php?>


does not actually cause an error or even a warning on 4.3.0 , not sure about previous versions though ?

I've never seen PHP script ended like that. At least I don't recall it being in the documentation.

Since you said it does not cause errors then either it is a bug or they included it in PHP but just really didn't mention it. As the script is parsed it would be read as:

php
?>

though technically the error would then be that is doesn't recognize the expressoin "php" maybe something like invalid expression on line XX.

applefool
03-11-2003, 07:19 PM
Well, i tried stopping and starting the services in the correct order. No changes from that. This is all taking place on a new install of Redhat 8, so there is no host holding me back here. I have tried changing the php?> still nothing.

I checked the info - PHP version 4.2.2, but there doesn't seem to be anything that says MySQL anywhere on page. MySQL is definitely set up and running, as I am using it on a CL.

Back to my original though, is there something I need to setup to get MySQL to work with PHP??

As a side note, unless i ssh into the redhat install, i can't access the MySQL on the redhat from any other machine internally or external.

firepages
03-12-2003, 03:19 AM
"but there doesn't seem to be anything that says MySQL anywhere on page. "

ok if no host did you compile PHP from source? - if so did you

./configure --with-mysql

if not mysql support wont be included and you will have to recompile with mysql support , you configuration options from your compilation should be shown in the phpinfo() output at the top of the page.

Spooks , I never saw anywhere that php?> was ok now have I seen it in the documentation either , just found out by accident that it happened to not hurt ?

applefool
03-12-2003, 11:52 AM
But the info page works, and other php scripts are working. Just doesn't seem to think that mysql is there... Also I didn't compile php or mysql as they were both included in the Redhat 8 install. Is there an entry in php.ini that points to mysql?

Thanks a lot for the ongoing help :)

Jason

firepages
03-12-2003, 05:00 PM
in your phpinfo output , the second info block should be called

"Configure Command" - post the contents of that , if mysql support was not enabled then you wont be able to use MySQL commands without recompiling PHP , I dont think (though wont swear) that you can run dynamically load the mysql libraries.

To recompile you will need the source rpms for apache mysql and php, so see if they are available on your CD's , recompiling really is much easier than it may sound at first.

let us know what you have and what you dont have.

applefool
03-12-2003, 05:58 PM
'./configure' '--host=i386-redhat-linux' '--build=i386-redhat-linux' '--target=i386-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/usr/com' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--prefix=/usr' '--with-config-file-path=/etc' '--enable-force-cgi-redirect' '--disable-debug' '--enable-pic' '--disable-rpath' '--enable-inline-optimization' '--with-bz2' '--with-db3' '--with-curl' '--with-dom=/usr' '--with-exec-dir=/usr/bin' '--with-freetype-dir=/usr' '--with-png-dir=/usr' '--with-gd' '--enable-gd-native-ttf' '--with-ttf' '--with-gdbm' '--with-gettext' '--with-ncurses' '--with-gmp' '--with-iconv' '--with-jpeg-dir=/usr' '--with-openssl' '--with-png' '--with-pspell' '--with-regex=system' '--with-xml' '--with-expat-dir=/usr' '--with-zlib' '--with-layout=GNU' '--enable-bcmath' '--enable-exif' '--enable-ftp' '--enable-magic-quotes' '--enable-safe-mode' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-discard-path' '--enable-track-vars' '--enable-trans-sid' '--enable-yp' '--enable-wddx' '--without-oci8' '--with-pear=/usr/share/pear' '--with-imap=shared' '--with-imap-ssl' '--with-kerberos=/usr/kerberos' '--with-ldap=shared' '--with-mysql=shared,/usr' '--with-pgsql=shared' '--with-snmp=shared,/usr' '--with-snmp=shared' '--enable-ucd-snmp-hack' '--with-unixODBC=shared' '--enable-memory-limit' '--enable-bcmath' '--enable-shmop' '--enable-versioning' '--enable-calendar' '--enable-dbx' '--enable-dio' '--enable-mcal' '--with-apxs2=/usr/sbin/apxs'


I am guessing I am going to have to recompile. Any easy instructions?


:rolleyes:

applefool
03-14-2003, 08:56 AM
It seems like --with-mysql is in there.. Do I need to recompile or do I have some other issues?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum