467,911 Members | 1,298 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,911 developers. It's quick & easy.

Cannot connect to Mysql

Hi, I hope you guys can help me out~~~
when I run it localhost/config.php, it cannot display any message such
as connect successfully or failed. Do u guys have any idea?

config.php
<html>
<head><title>Create Database and table</title></head>
<body>

<?php

$linkID = mysql_connect("localhost","","");
print ("hello"); <---can print
Hello

if($linkID != "FALSE") <--i think
fail here
{
print("The connection to the server was made successfully.)";
}
else
{
print("The connection to the server failed");
}

mysql_close($linkID);

?>
</body>
</html>

am I right? do i need to put username and password in
mysql_connect("localhost","","")? i dont know where i can find it, or
how can I find it in mysql. Hope you guys can help me out~~ Thanks.
Krista
Jul 17 '05 #1
  • viewed: 3908
Share:
2 Replies
Tom
Krista,

If you're using the default installation of MySQL (i.e. you haven't run a
grant statement to create a user), then you're script should have run. You
might want to try augmenting your connection statement with an error output
such as:

$linkID = mysql_connect("localhost","","") or die ('I cannot connect to the
database because: ' . mysql_error());

Tom

"Krista" <yw*****@hotmail.com> wrote in message
news:eb**************************@posting.google.c om...
Hi, I hope you guys can help me out~~~
when I run it localhost/config.php, it cannot display any message such
as connect successfully or failed. Do u guys have any idea?

config.php
<html>
<head><title>Create Database and table</title></head>
<body>

<?php

$linkID = mysql_connect("localhost","","");
print ("hello"); <---can print
Hello

if($linkID != "FALSE") <--i think
fail here
{
print("The connection to the server was made successfully.)";
}
else
{
print("The connection to the server failed");
}

mysql_close($linkID);

?>
</body>
</html>

am I right? do i need to put username and password in
mysql_connect("localhost","","")? i dont know where i can find it, or
how can I find it in mysql. Hope you guys can help me out~~ Thanks.
Krista

Jul 17 '05 #2
Krista wrote:

Hi, I hope you guys can help me out~~~
when I run it localhost/config.php, it cannot display any message such
as connect successfully or failed. Do u guys have any idea?

config.php
<html>
<head><title>Create Database and table</title></head>
<body>

<?php

$linkID = mysql_connect("localhost","","");
print ("hello"); <---can print
Hello

if($linkID != "FALSE") <--i think
fail here
{
print("The connection to the server was made successfully.)";
}
else
{
print("The connection to the server failed");
}

mysql_close($linkID);

?>
</body>
</html>

am I right? do i need to put username and password in
mysql_connect("localhost","","")? i dont know where i can find it, or
how can I find it in mysql. Hope you guys can help me out~~ Thanks.
Yes, you need to put in a username and password, which you should get from your
host. Also, you might want to use the "or die" syntax Tom suggested.
if($linkID != "FALSE") ^^^^^^^^^^ This can be better written as if (!$linkID)

or you could combine your IF and mysql_connect() statements:
if (!mysql_connect("localhost","username","pw"))
echo "Did not connect to DB";

And this line will produce an error if the connection didn't open correctly: mysql_close($linkID);

You can get around that by using the mysql_connect() or die syntax or by
executing the mysql_close only if $linkID is valid. Or, if you're lazy, you
could just code it as @mysql_close($linkID). The "@" keeps any errors from
being written.

Regards,
Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by bettina | last post: by
reply views Thread by Kyle Goetz | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.