PHP/MySQL Connection


I have been working on my site, and incorporating PHP and MySQL into it, including developing my own Blog Script.

I have been able to connect to the database fine, if I put the mysql_connect in the same file that is using the database, but I’m having one issue.

When I put the connection (same as when in the other file, I tripple checked this), code into an outside file, and include that via include_once, then call a function (I.E: dbConnect(); ) it errors out, saying it cannot connect to the mysql server.
Exact Error: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)

As I said, I didn’t change any of the actual connection code, I just put it in another file (, and included that file. Most sites I have read, say this is the way to do this.

Does anyone know what the issue is, or if Dreamhost might just have some sort of protection against this for some reason or another?

Thank you,
Craig (Abyss, -vI-)


this could be a silly answer :stuck_out_tongue:

But I will just check the hostname, username, and password again. I will first echo the values before I connect to mysql database in the other file.

Save [color=#CC0000]$97[/color] (max discount) on dreamhost plans by using promo code: [color=#CC0000]97CRAZY[/color].

I always connect to MySQL through a server-side include, so that cannot be the problem; however, I don’t use a function for this. Perhaps that is the problem?

Simon’s website
Save $100 on 1-year plans with promo code [color=#CC0000]SCJESSEY100[/color] (details)

Oh believe me, I did. Twice or three times.

I even copied and pasted incase I was missing something. It works in one page, not in another.

In response to the second response, what do you do, just have the included file have the connection outside of a function, so it connects the moment it runs the include?

I’ll try that, maybe that will work.

It works now. The second response was right, it works that way. Thanks for the help! :slight_smile: