mysql connection problem!

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
bouboul
Forum Commoner
Posts: 28
Joined: Sat Oct 25, 2003 4:08 am

mysql connection problem!

Post by bouboul »

hello,

i've just been installing mysql daemon, it is running nicely, but the problem arises when i'm trying to connect to mysql server using a php script . in myphpinfo() i can see something like
'--with-mysql=shared,/usr', i think that mysql should be supported. when i'm trying to connect i'm always getiing this:
Fatal error: Call to undefined function: mysql_connect() in /usr/local/httpd/htdocs/test.php on line 8.
here is the code i wrote :
<?php
//session_start();
//phpinfo();

$user="root";
$password="nabila";
$database="bouboul";
mysql_connect(localhost,$user,$password);
echo mysql_error();
?>
could anyone tell what is wrong with this ?

thanx
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

well, ur code says :

Code: Select all

<?php
//session_start(); 
//phpinfo(); 

$user="root"; 
$password="nabila"; 
$database="bouboul"; 
mysql_connect(localhost,$user,$password);
echo mysql_error(); 

?>

the problem is, you forgot to add apostraphies around localhost and ur 2 variables....

so, here is the way it should have been written:

Code: Select all

<?php

//session_start(); 
//phpinfo(); 

$user="root"; 
$password="nabila"; 
$database="bouboul"; 
mysql_connect('localhost',''.$user.'', ''.$password.'') or die(mysql_error()); 

?>

note that the '' around ''.$user.'' and ''.$password.'' are 2 apostraphies marks ( ' ). should work fine


last note, don't paste actual username/passwords in here.. for your own security :)

cheers
bouboul
Forum Commoner
Posts: 28
Joined: Sat Oct 25, 2003 4:08 am

Fatal error: Call to undefined function: mysql_connect()

Post by bouboul »

the code looked welll,
but My problem is that apache / php is not recognizing my mysql connects. WHen i look in php info i see that it is built --with-mysql . in my php.ini there is a line extension=mysql.so

pleaz help !!
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

well, i'm not really sure what you mean.. could you give me an example? but if you can put the code revision i just posted above in a php script, run it, and it doesn't give you an error, then you are connecting to mysql with no problem... you just gotta figure out what you want to do with that connection at that point..

if you have any error codes, that would help out too
bouboul
Forum Commoner
Posts: 28
Joined: Sat Oct 25, 2003 4:08 am

Post by bouboul »

i put the code revision you gave me here is the code :
<?php
$user="root";
$password="nabila";
$database="bouboul";
mysql_connect('localhost',$user,$password) or die(mysql_error());

?>
but the problem is that , when i'm tryng to run it i'm always getting the following :
Fatal error: Call to undefined function: mysql_connect() in /usr/local/httpd/htdocs/test.php on line 5.
i think that apache/php are not recognizing mysql functions. mysql daemon is running nicely alone as well as apache/php.
my problem is that i cannot connect to mysql server.

pleaz help
thanx
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

mysql_connect is a PHP function, not a MySQL function, so your problem isn't with your MySQL setup.

Mark
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

try this, see what it returns

Code: Select all

<?php
	$user="root"; 
	$password="nabila"; 
	$database="bouboul"; 
    $link = mysql_connect("localhost", $user, $password)
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");
    mysql_close($link);
?>
Your problem is, you need to store the result of mysql_connect("localhost", $user, $password) somewhere which your weren't doing...i think :)

Mark
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

well, he doesn't actually *have* to put the code in a variable.

he could still call it with

Code: Select all

<?php

@mysql_pconnect("localhost", $user, $password) or die(mysql_error());
echo 'we made it';
?>
and let mysql close after the script ends.
Post Reply