How does PHP know where to look for MySQL database?(No path)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CallumD
Forum Newbie
Posts: 11
Joined: Wed May 03, 2006 12:36 am

How does PHP know where to look for MySQL database?(No path)

Post by CallumD »

Hi all,

I am running a distribution (Uniform Server 3.3), so I am coding in a development environment.

When I call a database from within PHP code, eg:

@mysql_connect("localhost", "username", "password")

How does it know the path to the database file? Will the database file need to be sitting in the same folder as the PHP files?

Thanks.
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

The MySQL database is a seperate service running on a server, usually the same server as your web server. When it tried to connect to MySQL using "localhost' and the username and password specifiied, it'll try to make a connection to MySQL on the same host that the server is being ran from. If it can connect, it will use the username and password to login giving you access to your SQL tables.
CallumD
Forum Newbie
Posts: 11
Joined: Wed May 03, 2006 12:36 am

Post by CallumD »

So PHP will look for MySQL server first, not for a specific database file?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

PHP doesn't care where MySQL stores the files, that's MySQL's job.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

CallumD wrote:So PHP will look for MySQL server first, not for a specific database file?
PHP doesn't have access to the database files. Granted it may be possible to write a script that attempts to access them, bypassing the MySQL server.

PHP connects to the database server through a socket, effectively logging in using the credentials you supply. Once a database connection has been established, you can send commands and receive results by using PHP functions which communicate with the server though this connection.
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

if mysql server is in any other host and running on some other port rather than default port, you can also put the port in 4th parameter as well.

mysql_connect(IP_OR_Host, Username, Password, PORT);
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

You'll need to use:

Code: Select all

mysql_select_db('dbname') or die('Could not select db')
before you attempt to use data from any specific database
Post Reply