Forum Moderators: coopster

Message Too Old, No Replies

Why is my database not connecting?

mysql database php

         

dotancohen

11:04 am on Jul 21, 2005 (gmt 0)

10+ Year Member



Hi all, on one of the scripts I have this code:

[code]
mysql_connect ("localhost", "database", "****") or die (mysql_error());
mysql_select_db ("dotan_stats");

$query = "TOP SECRECT QUERY!";
$result = mysql_query($query);

print "\n<!-- ".mysql_error()." -->";
[code]

And I get this in my HTML code:
<!-- No Database Selected -->

Does anybody see anything obvious that would cause it not to connect? Where can I begin to debug?

Dotan

[edited by: coopster at 12:47 pm (utc) on July 21, 2005]
[edit reason] removed url per TOS [webmasterworld.com] [/edit]

buriedUnderGround

12:44 pm on Jul 21, 2005 (gmt 0)

10+ Year Member



Perhaps try adding "or die (mysql_error())" after your MYSQL_SELECT_DB() to find out why the connection is not being made.

jatar_k

3:55 pm on Jul 21, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welco9me to WebmasterWorld dotancohen,

adding "or die (mysql_error())" to all of your queries and database interaction functions while in development is the way to go but...

another thing is if you have many of them on one page it can be somewhat frustrating to find whodunnit, so

add a little text to each

or die ("connect died: " . mysql_error())
or die ("select db died: " . mysql_error())
or die ("query1 died: " . mysql_error())

and so on

ergophobe

4:37 pm on Jul 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Acutally, what I usually do (though modified somewhat since I use a simple database abstraction layer to help with this) is something along these lines

or die("Error in " . __FILE__ . " line " . __LINE__ . " :: " . mysql_error());

Then I know exactly where it's coming from.

dotancohen

6:02 pm on Jul 21, 2005 (gmt 0)

10+ Year Member



I do have the mysql_error() in there, just on the next line:

print "\n<!-- ".mysql_error()." -->";

I do that because I don't want the script to die- I'm forced to test this on a live site. So I just output it in HTML comments. And I know exactly where it is, so I do not need anything fancy.

But what would prevent it from connecting?

Thanks.

Dotan

[edited by: coopster at 7:05 pm (utc) on July 21, 2005]
[edit reason] no urls per TOS [webmasterworld.com] [/edit]

ergophobe

8:08 pm on Jul 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just take the same code, add in all your debugging info and put it on the live server in a file named test.php. Should be the same problem and same solution all around.

Mr_Fern

5:32 am on Jul 22, 2005 (gmt 0)

10+ Year Member



Could you post part of the "top secret query"?

The error is generated after the query tries to get executed, so the problem seems to lie somewhere in the query.

Anyango

6:34 am on Jul 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey!

Are you sure that the php pages you are saving are on the same physical server as mysql database? i mean for example you have a virtual host or your code is on another server then mysql then first of all "localhost" won't work or even if i both the things are on same server as is the case most of the time, then check for user permissions.

I might be wrong but i notice a small thing which might be the problem and no one else noticed

mysql_connect ("localhost", "database", "****") or die (mysql_error());
mysql_select_db ("dotan_stats");

you are putting "database" in place where you need to specify username for mysql db, is that a valid mysql user on your server?

do you have a valid MySql user 'database'?

well, ok if it is, then check for permissions for this user, is that user allowed to connect to your stats database?

I think this is for sure Permissions Problem, either host permissions or user permissions.

Thank You

Kami